(jquery) help with ajax call

Hello

I have an ajax call that simply updates a database using the form values, when a button is pressed.

<script>
function personal(){
       $.ajax({
           type: "POST",
           url: "settings_personal.php",
           data:     "first_name=" + document.getElementById("first_name").value + "&last_name=" + document.getElementById("last_name").value + "&email=" +    
document.getElementById("email").value,
           success:  $('.save-button').replaceWith('<div class="save-button"><input onclick="personal()" name="update_profile" type="button" value="Saved" class="btn btn-success btn-large"/>&nbsp;<input name="reset" type="submit" value="Reset" class="btn btn-inverse btn-large"/>')
       });
}
</script>

the settings_personal.php page contains the sql statements for the update along with validation checks.
Any errors are stored in an array and if an error is detected it won’t perform the update however it will still be detected as a success.

How can I check the errors array using jquery?

The success method is not in terms of the php database work, but is in terms of a successful reply from the php page.

If you look at the jQuery.ajax page you’ll see that the success function receives data as the first parameter.
That data is just the text output from the php script.

So you could have the php page output “ok” or something similar when things go well, and anything else could be shown as an error.


success: function (data) {
    if (data === 'ok') {
        $('.save-button').replaceWith('<div class="save-button"><input onclick="personal()" name="update_profile" type="button" value="Saved" class="btn btn-success btn-large"/>&nbsp;<input name="reset" type="submit" value="Reset" class="btn btn-inverse btn-large"/>');
    } else {
        // data could be an error message, so show that instead
        // ...
    }
}