Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于数据库结果停止ajax代码运行_Php_Ajax_Codeigniter - Fatal编程技术网

Php 基于数据库结果停止ajax代码运行

Php 基于数据库结果停止ajax代码运行,php,ajax,codeigniter,Php,Ajax,Codeigniter,我在一个名为table_data.php的视图中有一个名为save的引导对话框,我想确定是否将根据从数据库获得的结果来完成向数据库的Ajax发布 我只希望在home_model.php中的数据库查询不返回任何行时保存数据。然而,当我这样做时,它不起作用。这是一个cms,我正在使用codeigniter框架 我的页面是空白的。什么也没有出现。请帮帮我。我是网络新手,对JavaScript和php几乎没有经验,我刚刚开始使用ajax。非常感谢你的帮助 table_data.php视图 bootbo

我在一个名为table_data.php的视图中有一个名为save的引导对话框,我想确定是否将根据从数据库获得的结果来完成向数据库的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.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);
    }

我发现了一些语法上的小错误,但最大的问题是在ifcheck==0中尝试使用var检查的地方

您的条件评估ifcheck==0超出了用于检查的ajax调用的success函数。因此,ifcheck==0将在success函数运行之前执行,并为check设置一个值。如果您选择console.logcheck;在if语句之前,您会发现该值为“未定义”。此控制台结果也将记录在“console.logresponse;”输出之前这将确认执行顺序

换句话说,您需要决定是否在check_占用的ajax调用的success函数中运行下一个ajax调用

这是我的版本。这是未经测试的,但我认为这个概念是正确的。这仅显示Save按钮的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.');
  });
}

预订中$sql=SELECT*的语法错误,其中table_id=$tableid和session=$sessionid;忘记关闭双引号,但它仍然不起作用-问题似乎来自表_data.php视图什么问题?您检查控制台了吗?$.ajax{url:index.php/home/check_,type:post,data:{table_id:valueid2 session_id:table_column_15},成功:functionresponse{console.logresponse;var check=$response;} };问题似乎就在那里
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.');
  });
}