php - 数据库不使用 php 和 jquery 更新
问题描述
基本上,我希望根据 id 改变状态。没有错误,但没有更新。
索引.php
$(document).ready(function() {
$('.seatCharts-seat').click(function(){
var id = jQuery(this).attr("id");
$seatid = id;
$.post("class/booking.php",
{
seatid:$seatid
},
function(data, status){
console.log('data',data);
console.log('status',status);
console.log($seatid);
});
});
});
预订.php
<?php
require_once 'config.php';
class booking extends config{
public $id;
public function __construct($id){
$this->id = $id;
}
public function editTask(){
$con = $this->con();
$sql = "UPDATE `tbl_booking` SET `status`= 'unavailable' WHERE `id` = $this->id";
$data = $con->prepare($sql);
if($data->execute()){
return true;
}else{
return false;
}
}
}
?>
解决方案
选项 1 是创建一个通用文件来处理项目的所有 ajax 相关活动。假设我将其称为 myAjaxHandler.php
然后我将包含所需的类文件,并创建其实例以调用所需的方法。请记住,当您的项目变得更大时,维护起来会有点困难。
myAjaxHandler.php
<?php
require_once 'booking.php';
if(!empty($_POST) && isset($_POST['seatid'])) {
$booking = new booking($_POST['seatid']);
$result = $booking->editTask();
echo $result; // Would get 1 for true & 0 for false
}
ajax 的变化
$.post("path/to/myAjaxHandler.php",
{
seatid: $seatid
},
function(data, status){
console.log('data',data);
console.log('status',status);
});
if
选项 2 是在 booking.php 文件本身中包含上述块。
<?php
require_once 'config.php';
if(!empty($_POST) && isset($_POST['seatid'])) {
$booking = new booking($_POST['seatid']);
$result = $booking->editTask();
echo $result; // Would get 1 for true & 0 for false
}
class booking extends config { ... }
//Ajax
$.post("path/to/booking.php",
{
seatid: $seatid
},
function(data, status){
console.log('data',data);
console.log('status',status);
});
推荐阅读
- python - 如何根据另一列python将值附加到一行中?
- javascript - 在多维数组中将时间戳从 unix 更改为 MM/DD/YYY
- javascript - Firestore 请求缺少所需的身份验证凭据
- javascript - 每个循环的两个打字稿最佳解决方案
- git - 分叉存储库时对存储的影响
- c# - 无法从 JetBrains Rider 的单元测试中读取 app.config appSettings
- c++ - 如何检查字符串中的所有字符是空格还是字母?C++
- r - 如何将多个参数传递给R中的函数?
- python - 有没有办法为 DataFrame 的某些部分选择内部 True 值?
- visual-studio-code - 如何增加 Visual Studio Code 中的工作空间?