Page 1 of 1

Passing var to javascript

Posted: Tue Apr 08, 2008 3:46 pm
by csdavis0906
I am trying to use a javascript tooltip program (wz_tooltip.js) to show data held in a php var $summary. The javascript is called on the onmouseover event in the line of code below:

Code: Select all

echo '<td class="title"><a href=' . $url . ' onmouseover="return escape( ' . $summary .  ')">'.$title.'</td>';
The tooltip show with the word $summary in it not it. I need to convert the php var to a js var which the program supposedly will read & display.

Any help would be greatly appreciated. Thanking you in advance for your assistance...

Re: Passing var to javascript

Posted: Tue Apr 08, 2008 7:41 pm
by earcaraxe
The javascript escape function takes a string as its argument. If you're passing text into it, you need to wrap the variable in quotes, so that javascript sees it as a string.

Code: Select all

echo '<td class="title"><a href=' . $url . ' onmouseover="return escape('. "' $summary '".  ')">'.$title.'</td>';
Also, since you're trying to display a tooltip you don't want to return the escaped string. The code should really be:

Code: Select all

echo '<td class="title"><a href=' . $url . ' onmouseover="tip('. "' $summary '".  ')" onmouseout="UnTip()">'.$title.'</td>';
Also make sure that you include the javascript file at the beginning of the body section.

Code: Select all

<script type="text/javascript" src="wz_tooltip.js"></script>