Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble With Java Scripts and PHP

Status
Not open for further replies.

Treb

MIS
Apr 8, 2001
63
GB
Trouble With Java Scripts and PHP

Here's my Program :

<?

echo&quot;<head> \n<SCRIPT LANGUAGE=\&quot;JavaScript\&quot;>\n<!-- Begin \nfunction Start(page) { \nOpenWin = this.open(page, \&quot;CtrlWindow\&quot;, \&quot;toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width=680,height=460\&quot;);\n}\n// End -->\n</SCRIPT>\n</head>&quot;;


?> <font size=&quot;3&quot; face=&quot;Arial, Helvetica, sans-serif&quot;><a href=&quot;javascript:Start(' echo $title; ?>')&quot;;><? echo $eventtitle;?></a></font>&quot; ;?> <?

?>

How The Script Works:
It would generate a list of ie movie title affcrz that title would be clickbale. And When n user clicks that link a new window will appear..

This script is ok... but the thing is when n user pass variable to $event=Treb's World, the javascript function fails, it fails to open a new window why? bec of '. My Question is how to get out of this trouble.. Help me re write the code above... suggstn are welcome...
TNX
 
I believe you want something like this:

<head>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!-- Begin
function Start(page)
{
window.open(page,'CtrlWindow','toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width=680,height=460');
}
// End -->
</SCRIPT>
</head>
<?php
echo &quot;<font size=3 face='Arial, Helvetica, sans-serif'>&quot;;
$url = &quot;echo &quot;<a href=\&quot;javascript:Start($url)\&quot;>$eventtitle</a></font>&quot;;
?>

You can put the javascript code just oustide the php brackets, since you only send a variable to it. Is this what you want?
 
steijssen, thats exactly what i want.. it would perform the same as the above code...

it would work fine... unillll our n user passes something like this to $title variable..

what if n user passes: steijssen's world

The value of $url would be something like:
------------------------------------------------------
world
------------------------------------------------------
Right?

Here's our js and php func at work:
<?php
echo &quot;<a href=\&quot;javascript:Start($url)\&quot;>$eventtitle</a></font>&quot;;
?>

and when we pass the $url to the javascript function. it would result to this:

<a href=&quot;javascript:Start(' world')&quot;>steijssen's world</a>

i think u forgot the '' :)

anyway, ur script would work fine and my script also.. :) the thing is when u put ' on the $title var it wont open a new window..

TNX.. and i hope we solve this prob.. i really need this...
 
perhaps you could try to use the str_replace() function, and replace the ' with another character, like an underscore, when the user passes the variable:

$title = str_replace(&quot;'&quot;,&quot;_&quot;,$title);

and then when you catch the file do the same to put the ' back in the string, or continue using the underscore:

$title = str_replace(&quot;_&quot;,&quot;'&quot;,$title);
 
It's not a PHP problem, just a Javascript one. Yes, in delimiting with HTML and Javascript, one type of quote escapes another, but you can only go so deep into nested quotes that way. You are using double quotes around the main Javascript call, so you are using single quotes inside, but then obviously you can't have another single quote inside that.

The modern browsers are pretty forgiving: you could try not even having the outside pair of quotes. This kind of method works as long as there are no spaces in href={your_function}

Code:
<a href=javascript:Start(&quot;[URL unfurl="true"]http://www.domain.com/subdomain/diplayscript.php?title=<?[/URL] echo $title; ?>&quot;);>

This way, you don't need to use single quotes around your function argument; you can use double quotes, which then lets you use a single quote or apostrophe inside. You could also probably get away with

Code:
<a href='javascript:Start(&quot;[URL unfurl="true"]http://www.domain.com/subdomain/diplayscript.php?title=<?[/URL] echo $title; ?>&quot;)';>

(using one kind of quote to delimit the other)


On the other hand, Why not use the standard escape method of putting a backslash in front of your internal quotes.

Code:
<a href=&quot;javascript:Start(\&quot;[URL unfurl="true"]http://www.domain.com/subdomain/diplayscript.php?title=<?[/URL] echo $title; ?>\&quot;)&quot;;>

At least one of these should work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top