#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3

    Can Jquery Ajax check if a Query succeeded?


    I'm using Ajax to update a text in my database but I'm wondering if php can send a response back with Ajax if the Query is succesfull

    Code:
            $.ajax({
              type: 'POST',
              url: 'test.php',
              data: 'text=' + text,
    and on my test.php page

    Code:
    if($_POST){
    	try {
    		$insert_query = $db->prepare('UPDATE fields SET content = :content WHERE name = "homepage_mission" ');
    		$insert_query->execute(array('content' => $_POST['text']));
    	}
    	catch(PDOException $e){
    		print $e->getMessage();
    		die();
    	}
    }
    Help would be loved! thanks
  2. #2
  3. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    108
    Rep Power
    4
    Anything that your test.php code echoes will be snt back to the ajax call.

    Put
    echo "success";
    at the end of your code and
    echo "failure";
    in your error code.

    Then test the return with something like
    PHP Code:
    function fromAjax(resp) {
      if(
    resp === 'success') {
    // do success stuff.
      
    }
      else if(
    resp === 'failure') {
    // Do failure stuff
      
    }

    You can add a callback function to your AJAX call that runs fromAjax().
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Originally Posted by richpri
    Anything that your test.php code echoes will be snt back to the ajax call.

    Put
    echo "success";
    at the end of your code and
    echo "failure";
    in your error code.

    Then test the return with something like
    PHP Code:
    function fromAjax(resp) {
      if(
    resp === 'success') {
    // do success stuff.
      
    }
      else if(
    resp === 'failure') {
    // Do failure stuff
      
    }

    You can add a callback function to your AJAX call that runs fromAjax().
    Thank you! I didn't know that the success function of jquery ajax can fetch the echo's from php.

    I now got it working with success: function(returnMessage) .. And the returnMessage holds the echos from php. Perfect thx!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0
    I always format my PHP response to jQuery in a way so I can check for success, so:

    Code:
    $data = array(
        'success' => false,
        'results' => NULL,
        'error' => NULL
    );
    
    //DO a query here
    
    $q = "SELECT * FROM `something`";
    
    $result = mysql_query($q);
    
    if (mysql_num_rows($result) > 0) {
        $data['success'] = true;
        $data['results'] = mysql_fetch_assoc($result);
    } else {
       $data['error'] =  "No results";
    }
    
    echo json_encode((object)$data);
    Then in the jquery

    Code:
    success: function(data) {
        if (data.success) {
            //Do stuff
        } else {
            alert(data.error);
        }
    }
    This is a quick bad example, this tutorial will cover this example in more detail: jquery search

    Hope this helps!

    Comments on this post

    • richpri agrees : This should work quite well

IMN logo majestic logo threadwatch logo seochat tools logo