I have an AJAX call with jQuery that calls a PHP script, then returns some information in JSON format back to the success
or error
function like so:
$.ajax({
url: 'crud/clients.php',
dataType: 'json',
type: 'POST',
data: {
oper:'add'
,id:''
,clientID:$('#clientID_add').val()
},
async: false,
success: function(data){
alert(data.errorsExist);
},
error: function(data){
alert(data.appError);
}
});
Here is the PHP script being called:
try {
// Begin a transaction, turning off autocommit
$dbh->beginTransaction();
// Client INSERT
$sthClients->execute($crudColumnValues);
// Get the ID from the client we just INSERTED
$lastInsertID = $dbh->lastInsertId();
// Activity INSERT
$sthActivity->execute();
// commit the queries
$dbh->commit();
}
catch (PDOException $e) {
// Close the connection
$dbh = null;
// Echo back JSON
echo '{"appError": "'.$e->getMessage().'", "errorsExist":"Y"}';
// rollback the transaction
$testing = $dbh->rollBack();
exit();
}
When I test the error-handling and debug my code in my IDE, the PHP file stops at the rollBack();
call and my JSON never gets echoed back to my AJAX call after the rollback.
Is there any way to return that JSON back to my AJAX function when a rollBack
is called?