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?
SOLVED: Combining Javascript and php on same page
Moderator: General Moderators
SOLVED: Combining Javascript and php on same page
Last edited by rybrns on Sat May 12, 2007 12:50 pm, edited 1 time in total.
- andym01480
- Forum Contributor
- Posts: 390
- Joined: Wed Apr 19, 2006 5:01 pm
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.
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.
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:
...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.
I couldn't find a way to address JS inside the php or vice versa. Any help will be greatly appreciated.
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>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>');
?>
Last edited by rybrns on Sat May 12, 2007 12:55 pm, edited 1 time in total.
- andym01480
- Forum Contributor
- Posts: 390
- Joined: Wed Apr 19, 2006 5:01 pm
Something like this? $str will contain your javascript code calculate $result before it...
Then where you want your javascript code located.
Not tested and make sure the end of each heredoc is one a new line.
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;
?>Code: Select all
echo $str;Not tested and make sure the end of each heredoc is one a new line.
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:
And then in the php section of the <body> of the doc do this:
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.
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>Code: Select all
<?php
// ...code to calculate changing weeks, answer is $result...
$str = '<script language="javascript">';
$str .= 'buildIT("' . $result . '");';
$str .= '</script>';
echo $str;
?>