【问题标题】:Stopping ajax code from running based on result from database根据数据库的结果停止运行 ajax 代码
【发布时间】:2015-07-16 07:40:15
【问题描述】:

我有一个引导框对话框,在名为table_data.php 的视图中带有一个名为“保存”的按钮,我想根据从数据库获得的结果确定是否将 Ajax 发布到数据库。

我希望仅当home_model.php 中的数据库查询不返回任何行时才保存数据。但是,当我这样做时,它不起作用。它是一个 cms,我正在使用 codeigniter 框架。

我的页面是空白的。什么都没有出现。请帮我。我是网络新手,对 JavaScript 和 php 的经验很少,刚开始使用 ajax。您的帮助将不胜感激。

table_data.php(视图)

 bootbox.dialog({
      message: '<div class="row">  ' +
               '<div class="col-md-12"> ' +
               '<form class="form-horizontal"> ' +
               '<div class="form-group"> ' +
               '<label class="col-md-4 control-label" for="awesomeness">Table ID: </label> ' +
               '<div class="col-md-4">' +
               '<input id="idtable" type="text" value="'+table_column_15+'"/>' +
               '</div><br>' +
               '</div>'+
               '</form> </div>  </div>',
      title: "Form",
      buttons: {
        success: {
          label: "Save",
          className: "btn-success",
          callback: function() {
                                console.log('save');
                                console.log($('#re_confirm')[0].checked);
                                var valueid = document.getElementById('idtable').value
                                if(valueid == 0)
                                  myFunction();
                                var valueid2 = document.getElementById('idtable').value
                                if(valueid2==0)
                                  return;

                                 $.ajax({
                                    url: "<?php echo base_url(); ?>index.php/home/check_occupied",
                                    type: "post", // To protect sensitive data
                                    data: {
                                            "table_id" : valueid2
                                            "session_id" : table_column_15
                                        //and any other variables you want to pass via POST
                                          },
                                    success:function(response){
                                    // Handle the response object
                                       console.log(response);
                                       var check = $(response);
                                     }
                                  });

                                   if(check==0)
                                    return;

                                   $.ajax({
                                   url : "<?php echo base_url(); ?>index.php/home/update_booking",
                                   type: "post",
                                   data: {
                                       "table_id" : $('#idtable').val(),
                                   },
                                   success: function(response){
                                       ...
                                   }
                               });
          }
        },
        ...
        ,
        ...
      }
    });

home_model.php(模型)

  public function check_occupied($tableid,$sessionid)
        {

            $sql = "SELECT * FROM booking WHERE table_id=$tableid and session=$sessionid;
            $query = $this->db->query($sql);

            if ($query->num_rows() > 0)
                $imp = 1;
            else
                $imp = 0;

            return $imp;
        }

home.php(控制器)

public function check_occupied()
    {

        $tableid = $_POST['table_id'];
        $sessionid = $_POST['session_id'];
        $imp = $this->home_model->check_occupied($tableid,$sessionid);

        $this->load->view('table_data', $imp);
    }

【问题讨论】:

  • $sql = "SELECT * FROM booking WHERE table_id=$tableid and session=$sessionid"; 的语法错误忘记关闭双引号
  • 但它仍然不起作用 - 似乎问题来自 table_data.php(视图)
  • 什么问题??你检查过你的控制台吗??
  • $.ajax({ url: "index.php/home/check_occupied", type: "post",data: { "table_id" : valueid2 " session_id" : table_column_15 }, success:function(response){ console.log(response); var check = $(response); } });
  • 问题似乎在那里

标签: php ajax codeigniter


【解决方案1】:

我发现了一些语法上的小错误,但最大的问题是您尝试在 if(check==0) 中使用 var check

您的条件评估if(check==0) 超出了对 check_occupied 的 ajax 调用的成功函数。因此,if(check==0) 将在成功函数运行之前执行并为check 设置一个值。如果您在if 语句之前console.log(check);,您会发现该值是“未定义”。此控制台结果也将在 `console.log(response);' 的输出之前记录下来这将确认执行顺序。

换句话说,您需要在 check_occupied ajax 调用的成功函数内决定是否运行下一个 ajax 调用。

这是我的版本。它未经测试,但我认为这个概念是合理的。这仅显示“保存”按钮的 callback:

callback: function () {
  console.log('save');
  console.log($('#re_confirm')[0].checked);
  var valueid = document.getElementById('idtable').value;
  if (valueid === 0) {
    myFunction();
  }
  var valueid2 = document.getElementById('idtable').value;
  if (valueid2 === 0) {
    return;
  }
  $.ajax({
    url: "<?php echo base_url(); ?>index.php/home/check_occupied",
    type: "post", // To protect sensitive data
    data: {
      "table_id": valueid2,
      //??? where is table_column_15 declared and initialized? Some global var?
      "session_id": table_column_15
        //and any other variables you want to pass via POST
    },
    success: function (response) {
      // Handle the response object
      console.log('response='+response);
      //if I read check_occupied() right, response should only be 1 or 0 
      //there is no need to assign it to another var, eg. var check = response
      //there is no apparent need to turn it into a JQuery object with $(response) either
      if (response > 0) {
        $.ajax({
          url: "<?php echo base_url(); ?>index.php/home/update_booking",
          type: "post",
          data: {
            "table_id": $('#idtable').val()
          },
          success: function (response) {
          }
        });
      }
    }//end of success function callback for check_occupied() ajax
    console.log('ajax to check_occupied is done.');
  });
}

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多