PHP variables returning to "default" in subsequent AJAX calls

I am trying to use the set variable in another if, but it continues to return undefined. This is the PHP code:

if(isset($_POST['bookingTime'])){
    $bookingTime = $_POST['bookingTime'];
    $bookingDate = $_POST['theDate'];
    $dbDateTime = $bookingDate." ".$bookingTime;
    echo json_encode($dbDateTime);
    exit;
}

if(isset($_POST['bookingPwd'])){
    $pwd = $_POST['txtBookingPwd'];
    //Get the $dbDateTime variable???
    echo $pwd;
}

The variable $dbDateTimeis set as the response to the AJAX request and returns the jQuery value only in order, but I need a variable to query the database. How to use this variable in another if statement? I feel like I am missing something really basic in my understanding of php. I tried with globals but no luck.

Update

Removal exitdoes not matter. $dbDateTimethe variable is still not displayed in the second if statement. This is the PHP code I tested:

if(isset($_POST['bookingTime'])){
    $bookingTime = $_POST['bookingTime'];
    $bookingDate = $_POST['theDate'];
    $dbDateTime = $bookingDate." ".$bookingTime;
    // echo json_encode($dbDateTime);
    // exit;    
}

if(isset($_POST['bookingPwd'])){
    $pwd = $_POST['txtBookingPwd'];
    //Get the $dbDateTime variable???
    echo $dbDateTime;
    echo $pwd;
    $booking->newBooking($dbDateTime, $pwd);
}

AJAX , HTML. , . , , PHP , ?

, $dbDateTime AJAX. jQuery:

var bookingTime;
var dbDatetime;

$('.courtBook').click(function() {
    // Find the row
    var $row = $(this).closest('tr');
    // Find the text
    var bookingTime = $row.find('.courtTime').text()+":00";
    var theDate = $('#theDate').text();

    $.ajax({
        url: 'index.php?page=booking', 
        type: 'POST',
        dataType: 'json',
        data: {'bookingTime': bookingTime, 'theDate': theDate},
        success: function(data){
            var dbDatetime = data;
            alert(dbDatetime);
        },
        error: function(){
            alert('Much wrong, such sad');
        }
    });
});

$('#btnBookingPwd').click(function(){
    console.log(bookingTime);
    console.log(dbDatetime);
});

undefined. , jQuery?

+4
1

HTML- PHP script .

, AJAX HTML, PHP script. , PHP , . AJAX , , PHP .

, dbDateTime AJAX, , .

AJAX . var :

    success: function(data){
        var dbDatetime = data;
        alert(dbDatetime);
    },
0

Source: https://habr.com/ru/post/1621544/


All Articles