0

There is a numeric variable(variable name score below) which is a javascript variable. I have a php code inside which the javascript blocks have been used. Now what I want to do is: I want to get the actual value of score variable into my php code, and then save it into DB through the php code. I have tried the below code to do so. But I don't see any value coming into the $score variable declared in php and I simple see some error in the web page. Below the code there is a screenshot that explains the error message.

function check() {
                    var score = 0;
                    var wrong_answers="";
                    for(count = 0; count < 2; count++){
                        var e = document.getElementById(count + 1);
                        var answer_selected = e.options[e.selectedIndex].value;
                        if(e.selectedIndex === parseInt(allAnswer[count])){
                          score = score + 1;  
                        }
                        else{
                            wrong_answers = wrong_answers +" "+ (count+1).toString();
                        }
                    }
                     
                  <?php
                    $score = "<script>document.write(score)</script>";
                    $query4 = "UPDATE USER_RESULT SET SCORE = '$score' WHERE START_TIME = 
                              '$startTime'";
                    $query_run4 = mysqli_query($conn,$query4);
                    ?>
                }

enter image description here

satarupa
  • 19
  • 1
  • 2
    I believe you need to learn about [server and client side code](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming). Once you understand the difference, [get started with AJAX](https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX/Getting_Started). – El_Vanja Dec 08 '20 at 16:15
  • 2
    Also, glossing over [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) would be wise – Lawrence Cherone Dec 08 '20 at 16:19
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Dharman Dec 09 '20 at 01:04

1 Answers1

-1

I suggest you use AJAX to run it. (Requires JQuery)

Script.js:

function RunPHP() {
    $.ajax({
        async: false,
        type: 'GET',
        url: 'PHP.php'
    });
}

function check() {
                    var score = 0;
                    var wrong_answers="";
                    for(count = 0; count < 2; count++){
                        var e = document.getElementById(count + 1);
                        var answer_selected = e.options[e.selectedIndex].value;
                        if(e.selectedIndex === parseInt(allAnswer[count])){
                          score = score + 1;  
                        }
                        else{
                            wrong_answers = wrong_answers +" "+ (count+1).toString();
                        }
                    }
                     
                }

PHP.php:

  <?php
    $score = "<script>document.write(score)</script>";
    $query4 = "UPDATE USER_RESULT SET SCORE = '$score' WHERE START_TIME = 
              '$startTime'";
    $query_run4 = mysqli_query($conn,$query4);
    echo $query_run4;
  ?>
Dharman
  • 30,962
  • 25
  • 85
  • 135