0

I'm creating a basic web application project and I'm stuck here: What I am trying to do is to pass javascript variable (which has value of php variable) into jquery code (in external file) from my main .php file.

?>
    <script type="text/javascript" src="js/functions.js"></script>
    <script type="text/javascript">
    correct_answer = "<?php echo $_SESSION["correct_answer"]; ?>";
    </script>
<?php

It's working on the first time but since this part of website is also dynamically generated value of "correct_answer" is not refreshed. Is there a way to pass that "new" value into external jquery code?

//reload content of div
$(document).on('click', "#reload_btn", function() { 
  $("#test_main").load("includes/practise.inc.php #test_main > *");
});

//do something on dynamically added button
$(document).ready(function() {
  $(document).on('click', "#yes-btn", function() {
    console.log(correct_answer); //here is the problem - it either has the first value or is "undefined"
    $("#yes-btn").css( "background-color", "green");
    $("#yes-btn").css( "color", "white");
    $("#no-btn").css( "background-color", "red");
    $("#no-btn").css( "color", "white");
  });
});

Thank you in advance!

  • What do you mean by "external jquery code"? You could always load the most current value through AJAX, or add it to the responses of the existing AJAX requests – Nico Haase Oct 25 '22 at 12:13
  • What I meant by external jquery code is "external JS file with jquery code inside" - sorry for my mistake – ZuooMan Oct 25 '22 at 12:18
  • If you could kindly show an example how to do that I would be grateful. – ZuooMan Oct 25 '22 at 12:34
  • The scenario is a little unclear. Are you saying that when `.load("includes/practise.inc.php` is executed, the PHP code updates `$_SESSION["correct_answer"]` to a new value, and you want to get that new value back into your JavaScript? – ADyson Oct 25 '22 at 12:37
  • Yes, that is correct - this project of mine is a little quiz; there are 3 buttons: answer "YES" answer "NO" and button which reloads that specific div to get another question (PHP session variables are updated with no problem) – ZuooMan Oct 25 '22 at 12:40
  • I see. Well then, you definitely don't want to have the correct answer stored anywhere in the JavaScript, because people can easily use it to cheat. Anything you load on the client-side is visible to anyone who wants to look (using the browser's built-in tools). Instead, have your page submit the user's answer back to PHP, and then PHP (secretly, because it's server-side) compare it to the correct answer and then return a true/false response depending on whether the user was correct or not. – ADyson Oct 25 '22 at 12:47
  • That is something I missed but I don't really want to edit my code since its pretty much done (unless I have to do so) – ZuooMan Oct 25 '22 at 13:05
  • You don't _have_ to but it will a) make this task easier, and b) make your application more secure, so it's probably a good idea. – ADyson Oct 25 '22 at 13:16
  • Your other option is to launch another AJAX request to the server to get the new Session value, so in either case you're going to end up writing an AJAX request at the very least. – ADyson Oct 25 '22 at 13:17
  • I have tried solution from this question: https://stackoverflow.com/questions/47701392/retrieve-the-session-value-and-pass-as-required-parameter-of-api-ajax but it's the same result – ZuooMan Oct 25 '22 at 13:40
  • which solution did you try? The accepted answer there is the same as what you're already doing. And anyway this is the opposite of what you wanted - they are trying to get an existing value and send it to the server, whereas you're trying to get a new value _from_ the server. – ADyson Oct 25 '22 at 13:46
  • That might be the case... ehhh, I don't really think straight anymore today. Going to take a break and will be back into this tomorrow. Thanks for any help so far. – ZuooMan Oct 25 '22 at 13:52

0 Answers0