Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having an issue with an ajax request. I have a created a quiz which goes through questions and when the user finishes it, it should call a request to an AJAX.PHP file to call out information. Please see my code below, i have run the ajax.php file and it is gathering up all the information. But in the tests.php page, when it finishes it shows nothing. also the ajax $("#quiz_form,#demo1").addClass("hide"); seems to work as nothing displays, but doesnt complete the next two functions below.

Can someone help?

AJAX REQUEST - in tests.php

What I have tried:

function submit(){
     $.ajax({
                    type: "POST",
                    url: "ajax.php",
                    data: $('form').serialize(),
                    success: function(msg) {
                      $("#quiz_form,#demo1").addClass("hide");
                      $('#result').show();
                      $('#result').append(msg);
                    }
     });


ajax.php

$response= $db->prepare("SELECT id,question_name,answer FROM questions");
$response->execute();

 $i=1;
 $right_answer=0;
 $wrong_answer=0;
 $unanswered=0;

 while($result=$response->fetch(PDO::FETCH_ASSOC)) {
// while($result=mysql_fetch_array($response)){ 
       if($result['answer']==$_POST["$i"]){
           $right_answer++;
       }else if($_POST["$i"]==5){
           $unanswered++;
       }
       else{
           $wrong_answer++;
       }
       $i++;
 }
 echo "<div id='answer'>";
 echo " Right Answer  : ". $right_answer."<br>";

 echo " Wrong Answer  : ". $wrong_answer."<br>";

 echo " Unanswered Question  : ". $unanswered."<br>";
 echo "</div>";
Posted
Comments
Kornfeld Eliyahu Peter 22-Apr-18 10:45am    
How you can tell that the ajax call succeeded? Have you used you debugger (in-browser)?
Member 13637584 22-Apr-18 10:47am    
ive tried it all, even a alert message etc which brings me back what i want? Its just not displaying on the page like it should..
Richard Deeming 24-Apr-18 9:58am    
Is that submit function called as an event handler for the form's submit event?

If it is, then you'll need to prevent the default behaviour from firing; otherwise, the form will be posted to the server, the page will reload, and your AJAX call will not complete.

$("form").submit(function(e){
    e.preventDefault();
    $.ajax(...
});

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900