stribor40 Posted August 2, 2013 Share Posted August 2, 2013 i have ajax call that send data to the pph script which inserts it into database. If for some reasons I have error when connecting to database ot error on insertion how do i sent some sort of error to ajax. <?php error_reporting(0); $a = $_POST['user']; $b = $_POST['name']; $c = $_POST['comment']; $link = mysql_connect('localhost', 'rot', 'joker1'); if (!$link) { $arr = array('connect' => 0); //die('Could not connect: ' . mysql_error()); } header('Content-type: application/json'); echo json_encode($arr); ?> This is the only way i was able to pass json back to ajax. Can anyone suggest better way to do this. I would like to keep die statement but in order to get it working i had to comment it off Link to comment https://forums.phpfreaks.com/topic/280737-sql-error-return-json-to-jquery/ Share on other sites More sharing options...
Joshua F Posted August 2, 2013 Share Posted August 2, 2013 Seems like you kind of got the idea of how I'd do it. Something like this should work.. Note that I added mysql_error() into the array it's self. <?php error_reporting(0); $a = $_POST['user']; $b = $_POST['name']; $c = $_POST['comment']; $link = mysql_connect('localhost', 'rot', 'joker1'); if (!$link) { $arr = array( 'error' => mysql_error() ); } header('Content-type: application/json'); echo json_encode($arr); ?> Link to comment https://forums.phpfreaks.com/topic/280737-sql-error-return-json-to-jquery/#findComment-1443081 Share on other sites More sharing options...
stribor40 Posted August 2, 2013 Author Share Posted August 2, 2013 how do i know when with ajax if there was an error. mysql_error() will spit out big string which i cant check from ajax? Link to comment https://forums.phpfreaks.com/topic/280737-sql-error-return-json-to-jquery/#findComment-1443275 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.