Homer_J
Homer_J

Reputation: 3323

jQuery Ajax form submit error handling

I have the following code:

var url = "save.php"; // the script where you handle the form input.   

$.ajax({
        type: "POST",
        url: url,
        data: $("#frmSurvey").serialize(), // serializes the form's elements.

        success: function(){
            alert('success');
          },
        error: function(){
            alert( "Request failed: " );
          }

        }); 

Now, this works fine when submitting data to the DB - however, when I deliberately change the save.php file so that data isn't submitted, I still get the success alert.

Code for the save file below:

$proc = mysqli_prepare($link, "UPDATE tresults SET ip = ?, browser = ?, q1 = ?, q2 = ?, q3 = ?, q4 = ?, q5 = ?, q6 = ?, q7 = ?, q8 = ?, q9 = ?, q10 = ?, q11 = ?, q12 = ?, q13 = ?, q14 = ?, q15 = ?, q16 = ?, q17 = ?, q18 = ?, q19 = ?, q20 = ?, q21 = ?, q22 = ?, q23 = ?, q24 = ?, q25 = ?, q26 = ?, q27 = ?, q28 = ?, q29 = ?, q30 = ?, q31 = ?, q32 = ?, q33 = ?, q34 = ?, q35 = ?, q36 = ?, q37 = ?, q38 = ?, q39 = ?, q40 = ?, q41 = ?, q42 = ?, q43 = ?, q44 = ?, q45 = ?, q46 = ?, q47 = ?, q48 = ?, q49 = ?, q50 = ?, q51 = ?, q52 = ?, q53 = ?, q54 = ?, q55 = ? WHERE respondent_id = ?;");
    mysqli_stmt_bind_param($proc, "ssiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiissi", $ip, $browser, $q1, $q2, $q3, $q4, $q5, $q6, $q7, $q8, $q9, $q10, $q11, $q12, $q13, $q14, $q15, $q16, $q17, $q18, $q19, $q20, $q21, $q22, $q23, $q24, $q25, $q26, $q27, $q28, $q29, $q30, $q31, $q32, $q33, $q34, $q35, $q36, $q37, $q38, $q39, $q40, $q41, $q42, $q43, $q44, $q45, $q46, $q47, $q48, $q49, $q50, $q51, $q52, $q53, $q54, $q55, $respondent_id);
    mysqli_stmt_execute($proc);
    $mysql_error = mysqli_error($link);
    if ($mysql_error!="") {
        printf("Unexpected database error: %s\n", $mysql_error);
        mysqli_stmt_close($proc);
        mysqli_clean_connection($link);
        exit();
    } else
    {
        mysqli_stmt_close($proc);
        mysqli_clean_connection($link);
        update_completion_status($respondent_id, 'Started');
        header("Location: index.php?r=".$rguid);
    }

Where am I going wrong?

Upvotes: 1

Views: 3405

Answers (2)

Optimus Prime
Optimus Prime

Reputation: 6907

You probably have to change the way your server responds and make it return a different HTTP header that implies an error rather than a 200 OK.

Check what your server returns, and see that it's returning 404 status for non-existing pages.

Useful comments,

When I do this, it doesn't save the data but the 'success' message is showing.

That success is different from success of your db queries. Thats success of ajax call. If you want to check errors of mysql, then you will need to do something like this answer. See how I return success or fail using the php file, and then process that response using jquery.

As shown in the answer, in link in above comment, success of ajax can be seen is clearly different from the success returned by my php. Keep a check in your php file if all your db queries executed successfully, if not return message fail, even if you return this message fail, ajax call returns success, because the ajax call was successfull of course. That's how you got to the php files.

Upvotes: 1

Ryan Cori
Ryan Cori

Reputation: 435

I think the fact that you aren't submitted data in the POST anymore doesn't mean it should automatically throw and error. I don't write PHP so I don't know what your save.php does but assuming it's a valid HTTP POST request, there shouldn't be anything illegal with sending data in a POST and then doing nothing with it.

Upvotes: 2

Related Questions