Jump to content

AJAX call sometimes stores the wrong value


vikiesakki

Recommended Posts

I have online examination portal, in which a user selects an answer and clicks next. At this point I make an AJAX call.

With each AJAX call I insert one row into the database. I'm storing marks as floats.

It should store either 1 or -0.25, but sometimes it stores 0, and I can't find the problem.

Here is my code:

Jquery Code:

var questin = $("#ques"+next).val(); var secin = $("#sect"+next).val();
$.ajax({
data:{ques:questin,answ:n,sec:secin},
method:'post',
async:false,
url:'result.php',
success:function(data){

}
});

PHP Code:

<?php
include
("action/db.php");
session_start();
$ques_id = $_REQUEST['ques'];
$mark = (float) $_REQUEST['answ'];
$sec = $_REQUEST['sec'];
$exam_date_id = $_SESSION['ex_da'];
$use_id = $_SESSION['uid'];
$run->db_open();
$query2 = mysql_query("SELECT * FROM useranswers WHERE question_id='$ques_id' AND user_id='$use_id'");
$no = mysql_num_rows($query2);

if($no==0){
$date = date("Y:m:d:h:m:s");
$query1 = mysql_query("INSERT INTO useranswers VALUES ('','$exam_date_id','$use_id','$ques_id',0,'$sec','$mark','0','0','$date')");
}else{
$query1 = mysql_query("UPDATE useranswers SET mark='$mark' WHERE question_id='$ques_id'");
}
$run->db_close();
?>

Link to comment
Share on other sites

That would mean the value of "n" from the Javascript would be 0 or something that cannot be converted to a number. Right?

 

So what is the actual value of n during those requests? Or alternatively, what does the AJAX request look like in terms of the form data submitted (which you should be able to get directly from the browser).

Link to comment
Share on other sites

So you're saying you tried making a bunch of requests, watching in the browser as each request gets sent, until you got one of them to insert 0 for a question? And the value of "n" in the browser (or the value of "answ" in the request data) is always 1 or -0.25?

 

Do you have email set up? Try something as simple as

if ($mark != 1 && $mark != -0.25) {
    mail("yourself@example.com", "mark={$mark}", var_dump($_REQUEST, true), "From: yoursendingaccount@example.com");
}
Then you'll get an email every time $mark doesn't have the right value, and it will include all the form data that was sent.

 

And you say it's a recent development? That must mean something has changed recently. Were there any changes to the site? Did your host upgrade PHP versions or change other configuration?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.