PDA

View Full Version : Problem with inserting PHP variables in Javasript


Y-STU-K
09-27-2002, 09:21 PM
I dunno if i'm in the right forum for this i guess i am cause i'm using PHP to outpuit this :)


Right then i'm trying to insert a PHP variable with a path as a string into some javascript to display an image in a popup window.

Heres the odd bit when i use a string in a PHP variable and output it into me javascript and then load it in a browser i get an error it unterminated string constant.

However if i just input the path into the javascript as a piece of plain text i don't get this error, its most strange can someone help me out with a way around this

Nightfire
09-27-2002, 09:30 PM
You have to echo the php into the script, do you do that? I've got php in loads of my javascripts

Y-STU-K
09-27-2002, 09:45 PM
yes its begin echo'd into my script

allow me to show u:

echo ("<a href=\"javascript:;\" onclick=\"MM_openBrWindow('images/maps/oingyboingy/ob1.jpg','','width=1024,height=768')\"><img src=\"$thumbs[0]\" alt=\"$row->m_name\" /></a>");


the above is using the actual path of the image and works fine, now if i use a php variable (see below) it with the same string inside taken from an array, btw it also throws up an error when the text is not contained in an array.

images/maps/oingyboingy/ob1.jpg

echo ("<a href=\"javascript:;\" onclick=\"MM_openBrWindow('$images[0]','','width=1024,height=768')\"><img src=\"$thumbs[0]\" alt=\"$row->m_name\" /></a>");

this one gives an error message about the string not being terminated correctly :E

mordred
09-28-2002, 12:41 AM
Have you made sure the values you want to echo really exist? Because depending on the error reporting configuration, trying to echo unknow variables generates a notice - could break your JS syntax.

On the other hand, I noticed that you have a linebreak between "height" and "=768". If that's not an error from copy-pasting, then it will surely throw an "unterminated string" error.

aCcodeMonkey
09-28-2002, 04:03 AM
You haven't escaped all of the double quotes properly.
Try making the script line a String Variable, then echoing the variable
Example:
$sText='<FONT class="PageTitle"><b>Welcome ' . $_COOKIE['FName'] . '.</b></font><br><br>';

echo $sText;

Take a look at the article:
PHP Strings (http://www.phpbuilder.com/manual/language.types.string.php)

Hope this helps... :cool:

mordred
09-28-2002, 02:32 PM
Originally posted by aCcodeMonkey
You haven't escaped all of the double quotes properly.


He did so. Where do you see an unescaped quote?

aCcodeMonkey
09-28-2002, 06:40 PM
I've run into the problem with multiple \" escapes and " ' "s in the echo() function.
I worked around the problem by switching to building a string and concatenating the variable using: ' . $myVariable . ' string operator syntax

For troubleshooting, by breaking down the Javascript string into several separate PHP strings, it is easier to identify what is causing the error.
Simply add each new string until the error is prompted.

Example:

html>
<body>
<?php
$thumbs = array('thumb1.jpg','thumb2.jpg','thumb3.jpg');
$row = 1;
$sText = $sText . '<a href="javascript: ';
$sText = $sText . 'onclick="MM_openBrWindow(\'images/maps/oingyboingy/ob1.jpg\',';
$sText = $sText . '\'\',\'width=1024,height=768\')">';
$sText = $sText . '<img src="' . $thumbs[0] . '" alt="' . $row . 'm_name"/>';
$sText = $sText . '</a>';
echo $sText;
?>
</body>
</html>
Output:

<html>
<body>
<a href="javascript: onclick="MM_openBrWindow('images/maps/oingyboingy/ob1.jpg','','width=1024,height=768')"><img src="thumb1.jpg" alt="1m_name"/></a></body>
</html>

There are also syntax errors in the supplied sample code. Such as "java script:;".

:cool:

mordred
09-29-2002, 02:58 PM
I think you are making things worse than they previously were. Look here:

Y-STU-K:

echo ("<a href=\"java script:;\" onclick=\"MM_openBrWindow('images/maps/oingyboingy/ob1.jpg','','width=1024,height=768')\"><img src=\"$thumbs[0]\" alt=\"$row->m_name\" /></a>");


your suggestion:

$sText = $sText . '<a href="java script: ';
$sText = $sText . 'onclick="MM_openBrWindow('images/maps/oingyboingy/ob1.jpg',';


You are accidentally introducing new syntax errors because you dropped a double quote. The "java script" part is due to the way this discussion board formats user input.

Y-STU-K
09-29-2002, 04:49 PM
Ah right yeah that seems to work now thanx :)