Welcome to WebmasterWorld Guest from 23.22.182.29

Forum Moderators: open

Message Too Old, No Replies

How to handle mysql error inside ajax?

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

Junior Member

5+ Year Member

joined:Nov 16, 2008
posts: 136
votes: 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!");
}
});
1:50 pm on May 17, 2012 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4965
votes: 10


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)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4965
votes: 10


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)

Junior Member

5+ Year Member

joined:Nov 16, 2008
posts: 136
votes: 0


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