我试图在另一个if语句中使用变量集,但它继续返回undefined.这是PHP代码:
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;
}
$dbDateTime变量被设置为对AJAX请求的响应,并且返回jQuery很好,但我需要变量用于数据库查询.如何在其他if语句中使用该变量?在我对PHP的理解中,我觉得我遗漏了一些非常基本的东西.我试过全局,但没有运气.
更新
删除出口没有区别. $dbDateTime变量仍未显示在第二个if语句中.这是我运行测试的PHP代码:
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不存储值,因为变量是在不同的操作中设置的?
第二次更新
会话有效,但尝试通过下一个AJAX调用传递$dbDateTime的值.这是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);
});
在最后一次单击功能中,变量未定义.那么,这在jQuery中如何工作?
解决方法:
每当您的AJAX代码在HTML页面中运行时,它都会对PHP脚本进行新的调用.每次调用PHP脚本时,变量都会根据代码以默认状态开始.如果你想使用一个AJAX调用中的变量到下一个,你必须在下次调用PHP脚本时发送它.
尝试将dbDateTime的值作为参数添加到后续的AJAX调用中,我认为您将走上正确的轨道.
更新
你的AJAX几乎就在那里.尝试删除var:
success: function(data){
var dbDatetime = data;
alert(dbDatetime);
},
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。