SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    (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.

    Code JavaScript:
    <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?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,713
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Jaynesh View Post
    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.
    The success method is not in terms of the php database work, but is in terms of a successful reply from the php page.

    Quote Originally Posted by Jaynesh View Post
    How can I check the errors array using jquery?
    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.

    Code javascript:
    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
            // ...
        }
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •