General Community > Scripting Help

Javascript in PHP file causing parse error

(1/4) > >>

Oldiesmann:
I am working on my new website, and am using a javascript function to open a certain window. However, when I echo everything, I get an error saying Parse error expecting `,'' or `;'' in .../header.php on line 75. The line it's complaining about is "return false;" (without the quotes). Any ideas why this is happening and what I can do to fix it?

Parham:
can you please provide the line of code that's giving the error (the lines before and after might also help :))

Oldiesmann:
Whoops... Sorry about that...

<?php
/***********
*Header.php*
***********/

echo'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    ...
<script language="Javascript">
function songcomment(song)
{
  window.open('comment.php?song='+song,'Comment on '.$song['songname'].','height=400,width=300,toolbar=no,status=no,scrollbars=yes,location=no,resizeable=no');
  return false; // this line is causing the error
}
   ...
</script>
   ...';
?>

Parham:
<?php
/***********
*Header.php*
***********/

print <<<EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    ...
<script language="Javascript">
function songcomment(song)
{
  window.open('comment.php?song='+song,'Comment on '.$song['songname'].','height=400,width=300,toolbar=no,status=no,scrollbars=yes,location=no,resizeable=no');
  return false; // this line is causing the error
}
   ...
</script>
   ...
EOF;

?>

let me explain to you why you got the error:  notice your echo function, your echo'ing using single quotes like this:

<? echo 'something'; ?>

if you use single-quotes to surround your text, you cannot by any means (unless you escape them), use single quotes inbetween.  Same deal goes for double-quotes.  If you do want to use the echo function, then you have to escape all occurances of single quotes inbetween like this:

<?php
/***********
*Header.php*
***********/

echo'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    ...
<script language="Javascript">
function songcomment(song)
{
  window.open(\'comment.php?song=\'+song,\'Comment on \'.$song[\'songname\'].\',\'height=400,width=300,toolbar=no,status=no,scrollbars=yes,location=no,resizeable=no\');
  return false; // this line is causing the error
}
   ...
</script>
   ...';
?>

If you don't understand, read the ending of my PHP Lesson #4

Oldiesmann:
Ah... There's my problem... I didn't escape the single quotes in the brackets...

* Oldiesmann goes to fix his code
Thanks! :)

Navigation

[0] Message Index

[#] Next page

Go to full version