YigalB Posted August 11, 2012 Share Posted August 11, 2012 I wrote a short quiz in which PHP code creates a mathematical question based on random parameters. The intention was to let JS to check the user's answers (via form) and compare it to the required results. How can I pass the required data from PHP to the JS? After all, PHP variables are not recognized by the client side when executing the JS. Quote Link to comment https://forums.phpfreaks.com/topic/266954-can-i-pass-php-variables-content-to-javascript/ Share on other sites More sharing options...
floridaflatlander Posted August 11, 2012 Share Posted August 11, 2012 I haven't done anything like that but I have added picture info to js. I did it just like it was html <div id="loadarea"> </div> while { echo '<a href=".$variable." rel="enlargeimage" rev="targetdiv:loadarea, trigger:click" title="'.$row['pic_info'].'" >'; } It uses a loop and loads the last eight pics and displays in loadarea. Quote Link to comment https://forums.phpfreaks.com/topic/266954-can-i-pass-php-variables-content-to-javascript/#findComment-1368664 Share on other sites More sharing options...
Mahngiel Posted August 11, 2012 Share Posted August 11, 2012 sure you can. but it will also then become visible in the source. best to use a session or ajax call Quote Link to comment https://forums.phpfreaks.com/topic/266954-can-i-pass-php-variables-content-to-javascript/#findComment-1368676 Share on other sites More sharing options...
xyph Posted August 11, 2012 Share Posted August 11, 2012 You can simply eval the equation. <script type="text/javascript"> function check() { var question = document.getElementById('question').innerHTML; var answer = eval(question); var input = document.getElementById('answer').value; var message = ''; if( answer == input ) { message = 'Correct'; } else { message = 'Incorrect'; } document.getElementById('message').innerHTML = message; } </script> Please answer the following: <span id="question">6+7</span> = <input type="text" name="answer" id="answer"><br> <input type="button" value="Check" onclick="check()"><br> <span id="message"></span> I'm not sure of the security issue of using eval in JavaScript. I don't image it's too bad, as any manipulation of the DOM by an attacker makes the usage of eval trivial. Quote Link to comment https://forums.phpfreaks.com/topic/266954-can-i-pass-php-variables-content-to-javascript/#findComment-1368690 Share on other sites More sharing options...
Drongo_III Posted August 12, 2012 Share Posted August 12, 2012 You might want to look into Json encoding your 'answers' array (if indeed that is what you have). Then you can set that string as a javascript var and parse it to retrieve an object of 'keys and values'. Then you can do with it as you like in your javascript to check the answers for each question. Quote Link to comment https://forums.phpfreaks.com/topic/266954-can-i-pass-php-variables-content-to-javascript/#findComment-1368758 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.