facarroll Posted February 19, 2012 Share Posted February 19, 2012 I believe this is a PHP problem rather than a javascript one, but stand to be corrected. The following code shows two echo statements, with the second one commented out. The uncommented statement inside the javascript will not work, because while it calls up the check.php file it does not send the associated parameters correctly. I have left the commented echo in because it works perfectly. What's wrong? I know that javascript works client side, but the relevant php code has already run so the javascript should work. <?php // code associated with a database query precedes this if ("{$row['passState']}" == 0) { ?> <script language="javascript" type="text/javascript" > <-- var newwindow; function popup() { newwindow = window.open('check/check.php?quizTitle=".urlencode($quizTitle)."','_blank','scrollbars=yes,top=0,left=0,width='+screen.width+',height='+screen.height); if (window.focus) {newwindow.focus()} } //--> </script> <?php echo '<form><input type="button" onClick="popup()" value="Check your answers"></form> '; //echo "<A HREF=\"check/check.php?quizTitle=".urlencode($quizTitle)."\" onClick=\"return popup(this, 'thoughts', 'scrollbars=1'); window.moveTo(0,0); window.resizeTo(screen.width,screen.height-100);\">Check your answers</A>."; } ?> My justification for presenting a full screen popup for students is so that they can see answers they have given in an earlier quiz. I find that if their popup is just a window over their working page, they can use the popup to just fill in their answers on a second attempt. Full screen mode will limit this. Quote Link to comment https://forums.phpfreaks.com/topic/257328-problem-linking-to-a-file-when-using-javascript-in-php/ Share on other sites More sharing options...
codebyren Posted February 20, 2012 Share Posted February 20, 2012 Where your javascript code is being printed out, you are outside of PHP tags - so "urlencode" means nothing and the periods to concatenate your string also mean nothing. You need to "re-enter PHP" so functions etc. are recognised: var my_javascript_url = 'check/check.php?quizTitle=<?php echo urlencode($quizTitle); ?>'; Quote Link to comment https://forums.phpfreaks.com/topic/257328-problem-linking-to-a-file-when-using-javascript-in-php/#findComment-1319033 Share on other sites More sharing options...
requinix Posted February 20, 2012 Share Posted February 20, 2012 To be pedantic, you should also addslashes() for JavaScript string issues and htmlentities() for HTML issues. htmlentities(addslashes(urlencode($quizTitle))) Quote Link to comment https://forums.phpfreaks.com/topic/257328-problem-linking-to-a-file-when-using-javascript-in-php/#findComment-1319035 Share on other sites More sharing options...
facarroll Posted February 20, 2012 Author Share Posted February 20, 2012 Thanks. That looks good. Cannot try out till tonight. I'll reply further then. Quote Link to comment https://forums.phpfreaks.com/topic/257328-problem-linking-to-a-file-when-using-javascript-in-php/#findComment-1319037 Share on other sites More sharing options...
facarroll Posted February 20, 2012 Author Share Posted February 20, 2012 I changed the code as follows as was suggested by behicthebuilder. It works well now on Chrome, Firefox and iexplore, but Safari, Netscape and Opera open the page but do not deliver any dynamic content. I'm using <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Can anyone make a suggestion? <?php // code associated with a database query precedes this if ("{$row['passState']}" == 0) { ?> <script language="javascript" type="text/javascript" > <-- var newwindow; function popup() { newwindow = window.open('check/check.php?quizTitle=".urlencode($quizTitle)."','_blank','scrollbars=yes,top=0,left=0,width='+screen.width+',height='+screen.height); if (window.focus) {newwindow.focus()} } //--> </script> <?php echo '<form><input type="button" onClick="popup()" value="Check your answers"></form> '; //echo "<A HREF=\"check/check.php?quizTitle=".urlencode($quizTitle)."\" onClick=\"return popup(this, 'thoughts', 'scrollbars=1'); window.moveTo(0,0); window.resizeTo(screen.width,screen.height-100);\">Check your answers</A>."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257328-problem-linking-to-a-file-when-using-javascript-in-php/#findComment-1319111 Share on other sites More sharing options...
requinix Posted February 20, 2012 Share Posted February 20, 2012 I changed the code as follows as was suggested by behicthebuilder. You did? I don't see how. Quote Link to comment https://forums.phpfreaks.com/topic/257328-problem-linking-to-a-file-when-using-javascript-in-php/#findComment-1319116 Share on other sites More sharing options...
facarroll Posted February 20, 2012 Author Share Posted February 20, 2012 Requinix. I did this... <?php if ("{$row['passState']}" == 0) { ?> <script language="javascript" type="text/javascript <!-- var newwindow; function popup() { newwindow = window.open('check/check.php?quizTitle=<?php echo urlencode($quizTitle); ?>','_blank','scrollbars=yes,top=0,left=0,width='+screen.width+',height='+screen.height); if (window.focus) {newwindow.focus()} } //--> </script> <?php echo '<form><input type="button" onClick="popup()" value="Check your answers"></form> '; } ?> And now I have a lovely full-screen popup window without standard controls. I have included a script to allow the user to return to the previous page. Despite the browser problems, I think this problem is solved. Quote Link to comment https://forums.phpfreaks.com/topic/257328-problem-linking-to-a-file-when-using-javascript-in-php/#findComment-1319120 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.