Welcome to WebmasterWorld Guest from 54.159.111.156

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Facebook like button

Problem with inserting URL into href="" with PHP

     
4:06 pm on Oct 29, 2011 (gmt 0)

5+ Year Member



HI All,

I am using facebook like button on most of my static html pages, but have a problem with one piece of the code that I am using. Take a look at the code below please:

<script type="text/javascript">
//<![CDATA[
document.write('<div id="fb-root"><\/div><fb:like href="http://<?php echo($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);?>" send="false" layout="button_count" width="70" show_faces="false" action="like" font="arial"><\/fb:like>');
(function() {
var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://connect.facebook.net/en_US/all.js#xfbml=1';
s1.parentNode.insertBefore(s, s1);
})();
//]]>
</script>


It all works fine and it does what its suppose to do, but when I open html page in the Dreamweaver the line wich contains php shows the syntax error of some sort
http://<?php echo($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);?>


Can anyone give me some advise on what should I do or what am I doing wrong.

Thanks for all your help in advance.
4:10 pm on Oct 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is the javascript from Google posted on a PHP file?

It is hard to comment without seeing your entire file, but if you are trying to run the js code as php that might be where the error is coming from.

If that is the case, close the php tag ?> before the <script> and open a new one after </script> using <?php
4:25 pm on Oct 29, 2011 (gmt 0)

5+ Year Member



I do not quite understand it sorry, how would that work?

Can the little php line be simple inserted within the entire script as it is? or if it not then is there any other better way of inserting current page url into href attribute
11:24 pm on Oct 29, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



...but when I open html page in the Dreamweaver the line wich contains php shows the syntax error of some sort


What does the error say? I don't know Dreamweaver, but presumably the PHP is only going to execute successfully if you are running this in a web server with PHP installed?

Can the little php line be simple inserted within the entire script as it is? or if it not then is there any other better way of inserting current page url into href attribute


Can you not simply use JavaScript's
location.href
? No need for PHP?
12:03 am on Oct 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I misunderstood your question.

I don't use dreamweaver myself but you can change that line to:

<?php echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>

which is the same thing, but dreamweave could find it acceptable.
4:42 pm on Oct 30, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



which is the same thing, but dreamweave could find it acceptable.


It is more usual to omit the parentheses with the echo statement (and in some cases it is necessary), however, in this case I think it really is the same. AFAIK Dreamweaver itself does not have a clue about PHP syntax - that is down to the underlying installation of PHP (if any). I don't think it is a PHP issue as such, but rather a local-setup issue. (?)
5:18 pm on Nov 1, 2011 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I think your only mistake is using Dreamweaver. :-)

Why do you need to write this out using Javascript? Can't you just create a small php include at the point at which you want the button?
11:05 pm on Nov 1, 2011 (gmt 0)

5+ Year Member



Thanks for your help I think I found the way around it.

Question to rocknbil:

How would you place this script into include.php file?

<script type="text/javascript">
//<![CDATA[
document.write('<div id="fb-root"><\/div><fb:like href="http://<?php echo($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);?>" send="false" layout="button_count" width="70" show_faces="false" action="like" font="arial"><\/fb:like>');
(function() {
var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://connect.facebook.net/en_US/all.js#xfbml=1';
s1.parentNode.insertBefore(s, s1);
})();
//]]>
</script>
11:57 pm on Nov 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think your only mistake is using Dreamweaver. :-)


Ditto :)
4:15 pm on Nov 2, 2011 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member




How would you place this script into include.php file?


Look at what it's doing. it's creating an element script, and storing it in s, then getting the first script tag in the document,

var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];

then defines the parameters of the created script,

s.type = 'text/javascript';
s.async = true;
s.src = 'http://connect.facebook.net/en_US/all.js#xfbml=1';

then inserts it before whatever is the first script element in the document,

s1.parentNode.insertBefore(s, s1);

then using document.write to write out the divs for the like button. If this is a PHP file, I stand corrected, you don't even need an include. Just drop it in where you need it.

<div id="fb-like"><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
<fb:like href="http://<?php echo($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);?>" send="false" layout="button_count" width="70" show_faces="false" action="like" font="arial"></fb:like>
</div>

I think the reason it's output in a script that way is to allow it to validate by wrapping it in CDATA tags (the FBML tags are not valid XHTML.) If you want to stay with your original, just drop your original chunk into an include and use the usual include syntax.

If it's NOT a php file, you could do the same with SSI - or . . . .

<script type="text/javascript" src="my-facebook.js"></script>

Then move it all into my-facebook.js (without the cdata and script tags, of course.)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month