首页 > 解决方案 > 数据库不使用 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;
        }
    }
}
?>

标签: phphtmljquerydatabase

解决方案


选项 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);
  });

推荐阅读