1

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?

5
  • success will always be triggered unless you return a status code that isn't 200, or data that doesn't match the required datatype. Commented Aug 12, 2013 at 18:20
  • Try introducing an error on your php page .. you will see the difference. Commented Aug 12, 2013 at 18:23
  • Thanks Kevin for the response - the changes I made to the save.php page mean that it doesn't save data (columns don't match) but I still get the success message. Commented Aug 12, 2013 at 18:50
  • Right, but the server is still returning 200. you need it to return something else. You're catching the error and then returning a string, the status code is still 200. Commented Aug 12, 2013 at 18:50
  • Hi Kevin, OK, any pointers on how one would go about ensuring returning something different? Commented Aug 12, 2013 at 19:25

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

7 Comments

Thanks for the reply - it is indeed returning 404 for non-existing pages. The issue with - I've modified the save.php file so that MySQL would throw an error when trying to submit - for example that the number of columns don't match. When I do this, it doesn't save the data but the 'success' message is showing.
I have added the comments to answer.
Thanks Optimus - that makes sense but I do have error checking in the PHP file. if ($mysql_error!="") {printf("Unexpected database error: %s\n", $mysql_error);} - is this because I'm processing the save file in the background?
Sorry I did not get your question. If $mysql_error!="", i.e. some error occurred, I would suggest using echo "fail";exit(); . Thus data returned to ajax is "fail", you check data=="fail" and tell the user that file could not be saved. Else if your db queries executed successfully, echo "success"; and in your ajax call, you keep a check, if data=="success", tell the user file was saved. Help you further in the morning.
Thanks again, I think I know what is going on - even though there is an error on the PHP page - the way I am handling it means it isn't stopping the index page from being processed nor stopping the Ajax request - I'll have a play with the code - thanks again for replying.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.