JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 5th, 2013, 07:20 AM
notflip's Avatar
notflip notflip is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 148 notflip User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 39 m 9 sec
Reputation Power: 1
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

Reply With Quote
  #2  
Old February 5th, 2013, 11:02 AM
richpri's Avatar
richpri richpri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Chicago
Posts: 49 richpri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 32 m 9 sec
Reputation Power: 1
Facebook
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().

Reply With Quote
  #3  
Old February 6th, 2013, 01:53 AM
notflip's Avatar
notflip notflip is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 148 notflip User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 39 m 9 sec
Reputation Power: 1
Quote:
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!

Reply With Quote
  #4  
Old February 6th, 2013, 04:48 AM
revillwebdesign revillwebdesign is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 9 revillwebdesign User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 47 sec
Reputation Power: 0
Facebook
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Can Jquery Ajax check if a Query succeeded?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap