string concept: is this for php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php12342005
Forum Commoner
Posts: 79
Joined: Mon Mar 21, 2005 3:35 am

string concept: is this for php?

Post 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?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

If you were'nt writing PHP, just writing pure text JS, how would you write it? Then make PHP generate that.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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>";
?>
php12342005
Forum Commoner
Posts: 79
Joined: Mon Mar 21, 2005 3:35 am

Post by php12342005 »

ok, it works well,
thanks.
Post Reply