0

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?

FastTrack
  • 8,810
  • 14
  • 57
  • 78

1 Answers1

3

Maybe because of this:

// Close the connection
$dbh = null;
.....
$testing = $dbh->rollBack(); // $dbh is null
galymzhan
  • 5,505
  • 2
  • 29
  • 45
  • Yep! That was the problem. Should I put `$dbh = null` after the `rollBack` or maybe I don't even need it at all...? – FastTrack Sep 19 '12 at 19:13
  • 1
    If you don't explicitly close the connection, PHP will automatically close it. You can remove `$dbh = null` – galymzhan Sep 19 '12 at 19:29