Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

[web] Error when printing javascript with PHP

Started by jon723 Aug 26, 2008 at 9:53 PM 10 replies 3.6k views
Original Post
jon723
jon723
I'm attempting to open a window and then print some information to that window using php echo statements. When I click the link that calls the function I get an error that says I'm missing an ")" at the end of the argument list. I don't know where I'm going wrong but from the looks of things I have the right about of parenthesis. echo "&#106avascript:imageWindow = window.open(\"\",\"Test\", \"width=300, height=300,scrollbars=yes\");"; echo "imageWindow.document.writeln(\"\");"; Can anyone see an error with this statement?? Jon723
www.lefthandinteractive.net
adam4813
adam4813
Not sure where the issue is except you do need to escape the $ i believe or else it will try to insert the value of $var and not print $var.
Hodgman
Hodgman
Quote:
Original post by jon723
echo "imageWindow.document.writeln(\"\");";

This looks wrong... this is PHP code, that is printing out JS code (that is sent from the server to the browser), which is supposed to print out PHP code (within the browser).
PHP code should never reach the browser - it should always be executed on the server.

Maybe this is what you want?
echo 'imageWindow.document.writeln("';createThumbs($var);echo '");';
jon723
jon723
I apologize, my first post was incomplete. I'm calling the code from an anchor tag which then calls the php which calls the &#106avascript. Here is the complete example:

<?php	function createThumbs($var)	{ 		if ($dir = opendir($var))               		{ 			printf("<table>");		   while(($file = readdir($dir)) !== false) 		   {			  if ( preg_match("/\.(jpg)$/", $file))			  { 			  	 $currFile = $var.'/'.$file;	 			 				  printf("<tr><td><img src=\"%s\" /></td></tr>", $currFile);			  }		   }			printf("</table>");			   closedir($dir);                          		}		else		{			echo 'Cannot open directory ' . $var . '
'; } } function showThumbs($var) { echo "javascript:imageWindow = window.open(\"\",\"Test\", \"width=300, height=300,scrollbars=yes\");"; echo "imageWindow.document.writeln(\"<?php createThumbs($var); ?>\");"; }?>'"><img src="{@image}" border="0" alt="House Image" />
www.lefthandinteractive.net
Hodgman
Hodgman
Your anchor tag seems to have a superfluous quote at the end. It looks like it will end up as:
...

where XXXX = "&#106avascript:imageWindow = window.open("","Test", "width=300, height=300,scrollbars=yes");echo "imageWindow.document.writeln("");

Secondly, you're still sending PHP code to the browser^^^


[EDIT] It may be useful to load the page in your browser, and then send us the HTML code (of the buggy anchor tag) from the view-source screen.

[Edited by - Hodgman on August 26, 2008 11:08:24 PM]
adam4813
adam4813
echo "imageWindow.document.writeln(\"\");";

this line has the nested php code escaped into it. change it to the following to have the php executed:

echo "imageWindow.document.writeln(" . . ");";
Feralrath
Feralrath
Quote:
Original post by Hodgman
Secondly, you're still sending PHP code to the browser^^^


No hes not, the php code with be replaces by the server with the value that is to be echoed. the php will never reach the browser, his code on that point is fine.

as for the problem

there is no need to nest the php tags just escape the string like so

echo "&#106avascript:imageWindow = window.open(\"\",\"Test\", \"width=300, height=300,scrollbars=yes\");";

echo "imageWindow.document.writeln(\"", createThumbs($var); ,"\");";

The nested php tags are probably what is causing the error.
also on this line

'">House Image

why are you trying to suppress errors on the img src ="{@image} is that supposed to be $image?

Hodgman
Hodgman
Quote:
Original post by Feralrath
Quote:
Original post by Hodgman
Secondly, you're still sending PHP code to the browser^^^

No hes not, the php code will be replaced by the server with the value that is to be echoed. the php will never reach the browser, his code on that point is fine.
I'm afraid that some PHP code does reach the browser, at least on my PHP server it does.
Here's my test case:
<?php function createThumbs( $var ){  return "bar";}$var = 'foo';echo "Original code: \n";echo "imageWindow.document.writeln(\"<?php createThumbs($var); ?>\");";echo "\n\n Fixed code: \n";echo "imageWindow.document.writeln(\"" . createThumbs($var) . "\");";?>
Output:
Original code: imageWindow.document.writeln("<?php createThumbs(foo); ?>"); Fixed code: imageWindow.document.writeln("bar");
Feralrath
Feralrath
Well its relative, its not php code thats reaching the browser its a sting that looks like php code.

Either way its caused by the nested <?php tags.
jon723
jon723
Quote:
Original post by Feralrath
Quote:
Original post by Hodgman
Secondly, you're still sending PHP code to the browser^^^


No hes not, the php code with be replaces by the server with the value that is to be echoed. the php will never reach the browser, his code on that point is fine.

as for the problem

there is no need to nest the php tags just escape the string like so

echo "&#106avascript:imageWindow = window.open(\"\",\"Test\", \"width=300, height=300,scrollbars=yes\");";

echo "imageWindow.document.writeln(\"", createThumbs($var); ,"\");";

The nested php tags are probably what is causing the error.
also on this line

'">House Image

why are you trying to suppress errors on the img src ="{@image} is that supposed to be $image?




I'm using Dreamweaver's Spry and when using XML dataset's you can reference attributes and tags by referencing the element in the XML file by using "{tagname}". When the html is parsed Spry does the job of filling in the correct data for the tag (the xml is flattened into a big array/table of values). If you want to see how I'm using it here is the page it's being used. (The first image under Newark is what's breaking things since it's the one im using as a test)
www.lefthandinteractive.net
jon723
jon723
I took a screen shot of the error I get from Firebug when I'm in Firefox and hovering my mouse over the first image.

Error message from firebug debugger
www.lefthandinteractive.net
ID Merlin
ID Merlin
Quote:
Original post by adam4813
echo "imageWindow.document.writeln(\"\");";

this line has the nested php code escaped into it. change it to the following to have the php executed:

echo "imageWindow.document.writeln(" . . ");";


This answer is almost correct. You don't need to escape to PHP, you are already running as PHP in the echo line. You probably need to have escaped quotes around the createThumbs code, though.

Try this:
echo "imageWindow.document.writeln(\"" . createThumbs($var) . "\");";

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.