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

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3

    Can 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. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,620
    Rep Power
    595
    It is not clear what you want to accomplish. If the query is initiated by a form submit then PHP can report success/failure directly. If the query is initiated by an Ajax call then the answer is yes.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0
    Code:
    $.ajax({
      type: 'POST',
      url: "test.php",
      data: { example: "text" },
      success: function(msg) {
        alert('success');
      }
    });
  6. #4
  7. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124
    yes

    whatever your php page outputs is sent back to the client (think normal page, but asked for by javascript).

    Now, it looks like you are using jQuery, which has a myriad of options for the ajax method.

    many of the call back functions take this responded info as an argument.

    A favourite for beginners is "complete", eg

    Code:
    $.ajax({
              type: 'POST',
              url: 'test.php',
              data: 'text=' + text,
              complete:function(a,b) {
              	if(b == 'success') {
              		var d = a.responseText; //this is the data the server 'sent' back
              		
              		//process d
              		
              		//eg $("body").append(d);
              		
              		//or
              		
              		//var json = eval("("+a+")") //if someone knows a cleaner way, please let me know! (dataType:JSON has never worked for me)
              		
              	} else {
              		//something went wrong
              	}
              }
    });
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Originally Posted by Bronzesoda
    Code:
    $.ajax({
      type: 'POST',
      url: "test.php",
      data: { example: "text" },
      success: function(msg) {
        alert('success');
      }
    });
    What is this 'msg' variable? I don't quite understand how or what PHP is sending back? Could you give me an example please?

    Thanks!
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Originally Posted by Northie

    many of the call back functions take this responded info as an argument.

    });[/CODE]
    Aha! i found it thanks to this. So it doesn't matter what the name of the argument is, it's always from the server. how nice! thanks !

IMN logo majestic logo threadwatch logo seochat tools logo