Page 1 of 1

SOLVED: Combining Javascript and php on same page

Posted: Wed May 09, 2007 2:11 pm
by rybrns
I'm just learning how to use php, and have written an index page that loads a particular page based on the current week. Each week loads a different page. The code works well. Now I want to combine that page with one that masks the link that loads the page from search engines. I currently do that with Javascript, using a document.write() directive that assembles the link when the cursor is placed over it.

The problem is that I can't figure out how to combine the link calculated in the php part of the page from the JS link-assembly part. The JS part is in a document.write() directive that takes the first part of the anchor, <a href="... , concatenates it with the actual page name, and then concatenates that with the last part, ....html">Go here</a>.

I've tried all kinds of approaches using "echo" to write JS statements inside php and trying JS statements and including the php result (using <?php $result ?>) embedded in the JS output. Nothing I've tried works.

Is this possible to do?

Posted: Wed May 09, 2007 2:26 pm
by andym01480
Shouldn't be a problem. PHP outputs HTML to the browser so there is no reason why it can't output javascript via an echo statement. Post some code.

Does the javascript really hide the link from search engines? Surely a robots.txt file in the top directory disallowing the files you want hidden would be more reliable.

Posted: Wed May 09, 2007 3:11 pm
by rybrns
Yes, the Javascript does work to make the link invisible to spiders. And very few web spiders obey robots.txt. I know, my logs show it.

Here is the JS fragment:

Code: Select all

<script language="javascript">
//...some code that calculates "result"...

	var ur,rl2,rl,end,anc,tx
	ur = '<a href=\"http://somesite.com/'
	rl2 = '.html'
	rl = result + rl2
	end = '\">'
	anc = '<\/a>'
	tx = ' Go here.'
	document.write(ur+rl+end+tx+anc)
</script>
...and this code assembles a link when the cursor is moved over it. Here is a fragment of the working php code. What I'd like to do is to mask the link in the same way that the JS code does (or do it in any other way that works.

Code: Select all

<?php

	$choice = array(
	array(2007,4,05,'week1'),
	array(2007,4,12,'week2'),
	array(2007,4,19,'week3'),
	array(0,0));

	$i = 0;
	$done = 0;

	while (($choice[$i][0] > 0) && ($done == 0)) {

		$month = $choice[$i][1]+1;
		$day = $choice[$i][2];
		$year = $choice[$i][0];
		$days = int((mktime(0,0,0,$month,$day,$year) - time(void))/86400);

  		if (($days < 7) && ($days > -1)) {
    		$result = $choice[$i][3];
     		$done = 1;
		}
  		$i++;
 	}

echo('<a href="http://somesite.com/' . $result  . '.html">Go here.</a>');

?>
I couldn't find a way to address JS inside the php or vice versa. Any help will be greatly appreciated.

Posted: Wed May 09, 2007 3:26 pm
by andym01480
Something like this? $str will contain your javascript code calculate $result before it...

Code: Select all

<?php
$str = <<<EOD //heredoc to create a string that spans several lines
<script language="javascript"> 
//...some code that calculates "result"... 

   var ur,rl2,rl,end,anc,tx 
   ur = '<a href=\"http://somesite.com/' 
   rl2 = '.html' 
   rl = 
EOD;
$str.=$result; //concatenate previously calculated result
$str.= <<<EOD1 //another heredoc to finish the javascript
 + rl2 
   end = '\">' 
   anc = '<\/a>' 
   tx = ' Go here.' 
   document.write(ur+rl+end+tx+anc) 
</script> 
EOD1;
?>
Then

Code: Select all

echo $str;
where you want your javascript code located.
Not tested and make sure the end of each heredoc is one a new line.

Posted: Wed May 09, 2007 4:06 pm
by rybrns
Tried this and several close variants, none work. Looking at the Javascript code in the page source, I see that the code was output to html and looks correct, but nothing is output to the screen.

Posted: Sat May 12, 2007 12:49 pm
by rybrns
OK, got it to work. Tuns out it was fairly simple, once I got the idea from andym01480; thanks!. So this is one way to pass the result of a php server-side calc to Javascript. Here's how I got it to work:

Put this in the <head> of the doc:

Code: Select all

<script language="javascript">
function buildIT(r1)  {

   ur = '<a href="http://somesite.com/';
   rl2 = '.html';
   end = '">';
   anc = '</a>';
   tx = ' Go here.';
   document.write(ur+rl+end+tx+anc);
}
</script>
And then in the php section of the <body> of the doc do this:

Code: Select all

<?php

// ...code to calculate changing weeks, answer is $result...

$str =  '<script language="javascript">';
$str .= 'buildIT("' . $result . '");';
$str .= '</script>';

echo $str;

?>
Apparently the Javascript code only seems to work if it is in the <head> (at least on my server). So I made the final php commands into a simple function call and passed the php $result via a parameter. I guess this solution is really basic stuff, but it took a while to see it.