Welcome to WebmasterWorld Guest from 54.197.168.70

Forum Moderators: open

How to handle mysql error inside ajax?

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

5+ Year Member



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!");
}
});
1:50 pm on May 17, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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!");
}
});
1:51 pm on May 17, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Note, where I have:
// there was an error
You would want to add code to handle that error
2:14 pm on May 17, 2012 (gmt 0)

5+ Year Member



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

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month