homepage Welcome to WebmasterWorld Guest from 54.161.192.135
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
How to handle mysql error inside ajax?
skoff




msg:4454572
 1:15 pm on May 17, 2012 (gmt 0)

Hi! So here's my question! I have this code that I use and it's working perfectly to insert data in my db. I know that jquery/ajax will handle some error like if the page isnt found or timeout etc. However if there's an error on server side while doing the insert in my table in jquery/ajax it will be successful no matter what. I guess that I need to send back something to confirm that it was well done or not. But I have no clue how to do this? Any help would be appreciated!

Thanks


$.ajax({
type: "POST",
url: "process/subscribe_process.php",
data: dataString,
success: function() {
$("#message").html("Everything's good!");
//I need something here to confirm if mysql_query was done or not
},
error: function() {
$("#message").html("Error!");
}
});

 

Fotiman




msg:4454583
 1:50 pm on May 17, 2012 (gmt 0)

You need to modify process/subscribe_process.php to return something. For example, here's some pseudo code:

<?php
// susbscribe_process.php
// insert into database
// if no problems
// echo "success";
// else
// echo "error";
?>

And then:

$.ajax({
type: "POST",
url: "process/subscribe_process.php",
data: dataString,
dataType: "text",
success: function(data) {
if (data == "success") {
$("#message").html("Everything's good!");
}
else {
// there was an error
}
},
error: function() {
$("#message").html("Error!");
}
});

Fotiman




msg:4454584
 1:51 pm on May 17, 2012 (gmt 0)

Note, where I have:
// there was an error
You would want to add code to handle that error

skoff




msg:4454600
 2:14 pm on May 17, 2012 (gmt 0)

great! that's what I was missing! Thanks for your help

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved