🔒 Locked
🤖 Godot
[web] Error when printing javascript with PHP
Advertisement
Original Post
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 "javascript:imageWindow = window.open(\"\",\"Test\", \"width=300, height=300,scrollbars=yes\");"; echo "imageWindow.document.writeln(\"\");"; Can anyone see an error with this statement?? Jon723
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 '");';
I apologize, my first post was incomplete. I'm calling the code from an anchor tag which then calls the php which calls the javascript. 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" />
Your anchor tag seems to have a superfluous quote at the end. It looks like it will end up as:
...
where XXXX = "javascript: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]
...
where XXXX = "javascript: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]
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 "javascript: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
'">
why are you trying to suppress errors on the img src ="{@image} is that supposed to be $image?
Quote:I'm afraid that some PHP code does reach the browser, at least on my PHP server it does.
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.
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) . "\");";?>Original code: imageWindow.document.writeln("<?php createThumbs(foo); ?>"); Fixed code: imageWindow.document.writeln("bar");
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 "javascript: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
'">
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)
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.
Advertisement
