homepage Welcome to WebmasterWorld Guest from 54.227.34.0
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Facebook like button
Problem with inserting URL into href="" with PHP
AlexB77

5+ Year Member



 
Msg#: 4381118 posted 4:06 pm on Oct 29, 2011 (gmt 0)

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.

 

Habtom

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4381118 posted 4:10 pm on Oct 29, 2011 (gmt 0)

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

AlexB77

5+ Year Member



 
Msg#: 4381118 posted 4:25 pm on Oct 29, 2011 (gmt 0)

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

penders

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



 
Msg#: 4381118 posted 11:24 pm on Oct 29, 2011 (gmt 0)

...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?
Habtom

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4381118 posted 12:03 am on Oct 30, 2011 (gmt 0)

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.

penders

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



 
Msg#: 4381118 posted 4:42 pm on Oct 30, 2011 (gmt 0)

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. (?)

rocknbil

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



 
Msg#: 4381118 posted 5:18 pm on Nov 1, 2011 (gmt 0)

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?

AlexB77

5+ Year Member



 
Msg#: 4381118 posted 11:05 pm on Nov 1, 2011 (gmt 0)

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>

Habtom

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4381118 posted 11:57 pm on Nov 1, 2011 (gmt 0)

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


Ditto :)

rocknbil

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



 
Msg#: 4381118 posted 4:15 pm on Nov 2, 2011 (gmt 0)


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.)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved