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'];
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;
}
if(isset($_POST['bookingPwd'])){
$pwd = $_POST['txtBookingPwd'];
echo $dbDateTime;
echo $pwd;
$booking->newBooking($dbDateTime, $pwd);
}
AJAX , HTML. , . , , PHP , ?
, $dbDateTime AJAX. jQuery:
var bookingTime;
var dbDatetime;
$('.courtBook').click(function() {
var $row = $(this).closest('tr');
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?