Page 1 of 1

string concept: is this for php?

Posted: Fri Jul 29, 2005 9:00 am
by php12342005
function Alert($str)
{
echo("<script language='javascript'>");
echo("alert('$str');");
echo("</script>");
}

//no error
$str="Hello peter";
Alert($str);

//error
$str="Hello peter's cats";
Alert($str);

error code is: Expected ';'
I guess problem is the ' in $str, which makes actural look in second line of fuction Alert() as:
"alert('Hello peter's cats');"

so quote perter's stops (ends) first quote alert('
is that true?
how do u solve this kind problem?

Posted: Fri Jul 29, 2005 9:20 am
by nielsene
If you were'nt writing PHP, just writing pure text JS, how would you write it? Then make PHP generate that.

Posted: Fri Jul 29, 2005 9:22 am
by timvw
You are missing a pair of quotes around your message string in the alert function... Here is how i tackle this problem usually :)

Code: Select all

<?php

$str = addslashes("Hello peter's cats");

echo "<script type='text/javascript'>";
echo "alert('$str')";
echo "</script>";
?>

Posted: Fri Jul 29, 2005 9:53 am
by php12342005
ok, it works well,
thanks.