The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Can Ajax check if a Query succeeded?
Discuss Can Ajax check if a Query succeeded? in the PHP Development forum on Dev Shed. Can Ajax check if a Query succeeded? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 5th, 2013, 07:18 AM
|
 |
Contributing User
|
|
Join Date: Sep 2012
Posts: 148
Time spent in forums: 18 h 39 m 9 sec
Reputation Power: 1
|
|
|
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
|

February 5th, 2013, 07:26 AM
|
|
|
|
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.
|

February 5th, 2013, 07:28 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 1
Time spent in forums: 9 m 28 sec
Reputation Power: 0
|
|
Code:
$.ajax({
type: 'POST',
url: "test.php",
data: { example: "text" },
success: function(msg) {
alert('success');
}
});
|

February 5th, 2013, 07:32 AM
|
 |
Square Peg in a Round Hole
|
|
Join Date: Oct 2007
Location: North Yorkshire, UK
|
|
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
}
}
});
|

February 5th, 2013, 07:44 AM
|
 |
Contributing User
|
|
Join Date: Sep 2012
Posts: 148
Time spent in forums: 18 h 39 m 9 sec
Reputation Power: 1
|
|
Quote: | 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!
|

February 5th, 2013, 07:46 AM
|
 |
Contributing User
|
|
Join Date: Sep 2012
Posts: 148
Time spent in forums: 18 h 39 m 9 sec
Reputation Power: 1
|
|
Quote: | 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 !
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|