Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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 如何更改复选框属性_Php_Jquery_Ajax_Mysqli - Fatal编程技术网

Php 如何更改复选框属性

Php 如何更改复选框属性,php,jquery,ajax,mysqli,Php,Jquery,Ajax,Mysqli,在数据库中包含日期列的表中,选中复选框。如果输入的日期已经在数据库中,如何禁用复选框。例如,如果表中包含日期=13/9/2019且复选框=h1,那么如果我们输入日期13/9/2019,复选框h1将被禁用,反之亦然 $(文档).ready(函数){ $(“#日期”)。更改(功能{ 变量日期=$(“#日期”).val(); $.ajax({ url:“variable.php”, 方法:“张贴”, 数据:{ 日期2:日期 }, 数据类型:“文本”, 成功:函数(html){ //在这里放什么? }

在数据库中包含日期列的表中,选中复选框。如果输入的日期已经在数据库中,如何禁用复选框。例如,如果表中包含日期=13/9/2019且复选框=h1,那么如果我们输入日期13/9/2019,复选框h1将被禁用,反之亦然

$(文档).ready(函数){
$(“#日期”)。更改(功能{
变量日期=$(“#日期”).val();
$.ajax({
url:“variable.php”,
方法:“张贴”,
数据:{
日期2:日期
},
数据类型:“文本”,
成功:函数(html){
//在这里放什么?
}
});
});
});


在mysqli_查询之后,执行以下操作

if (isset($_POST['date2'])){
    $query = mysqli_query($db, "SELECT * FROM mytable where date = '".$_POST["date2"]."'");
    if (mysqli_num_rows($query) > 0)
    {
        // print/echo or return(if a return-function) - true/1/successful
        // eg.
        print 1;
        exit();
    }
}
并在Ajax响应中获得结果

$.ajax({
  url: "variable.php",
  method: "POST",
  data: {
    date2: date
  },
  dataType: "text",
  success: function(html) {
    //what to put here?
    if (html == 1) {
        //disable checkbox
    }
  }
});
在您的php文件中放入

 <?php  $db = mysqli_connect('localhost','root','','mydatabase');
 if (isset($_POST['date2'])){
 $result = mysqli_query($db, "SELECT * FROM mytable where date = 
  '".$_POST["date2"]."'");
   if ($result) 
   { 
     // it return number of rows in the table. 
     $row = mysqli_num_rows($result); 

     if($row > 0){
          echo 1; //exist
     } else {
          echo 0; // not exist
     // close the result. 
     mysqli_free_result($result); 
   } 
 }
 ?>

您应该在ajax成功回调中返回true | false,并在成功回调中放置下面的代码

$.ajax({
  url: "variable.php",
  method: "POST",
  data: {
    date2: date
  },
  dataType: "text",
  success: function(result) {
   if(result == true || result == 'true'){ // # just to be safe side
       $("input[type='checkbox']").attr("disabled",true); // #disable the checkbox
    }
  }
});

首先,您的PHP脚本需要改进。您应该启用并使用准备好的语句。使用前还应验证该值

<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$db = new mysqli('localhost','root','','mydatabase');
$db->set_charset('utf8mb4');

if (isset($_POST['date2'])) {
    $date = (new DateTime($_POST["date2"]))->format('Y-m-d'); //validate format
    // prepare -> bind -> execute
    $stmt = $query = $db->prepare("SELECT 1 FROM mytable WHERE date = ? ");
    $stmt->bind_param('s', $date);
    $stmt->execute();
    // get a single column from the first row of the mysql result
    $exists = (bool)$stmt->get_result()->fetch_row();

    // send JSON response to AJAX
    echo json_encode(['exists' => $exists]);
}


ajaxsuccess:functionforajax呢?ajaxsuccess将返回响应。在您的例子中,它是
html
。您可以使用console检查响应。logI编辑了我的问题,因为它应该是复选框数组。如果我有很多复选框,如何指定要禁用的复选框?
fetch_row()[0]
如果找不到记录,快捷方式将导致未定义的索引错误。它应该是我编辑代码的方式,或者是查询中的计数(1)。PDO更好的另一个原因是——没有这样的怪癖:)@YourCommonSense我测试了它(PHP7.2.5),我无法看到错误消息。我一定是做错了什么。尽管如此,您提出的方法似乎更好。看,fetch_row()在找不到结果时返回false。我明白了,试图从false will@YourCommonSense访问数组元素,所以它只是在PHP7.4中添加的。糟糕。我对这种行为一无所知,甚至忽略了我自己的例子。我道歉。
$(document).ready(function {
  $("#date").change(function {
    var date = $("#date").val();

    $.ajax({
      url: "variable.php",
      method: "POST",
      data: {
        date2: date
      },
      dataType: "text",
      success: function(html) {
        if(html > 0){
          $('#checkbox').attr("disabled", true);
        }
      }
    });
  });
});
$.ajax({
  url: "variable.php",
  method: "POST",
  data: {
    date2: date
  },
  dataType: "text",
  success: function(result) {
   if(result == true || result == 'true'){ // # just to be safe side
       $("input[type='checkbox']").attr("disabled",true); // #disable the checkbox
    }
  }
});
<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$db = new mysqli('localhost','root','','mydatabase');
$db->set_charset('utf8mb4');

if (isset($_POST['date2'])) {
    $date = (new DateTime($_POST["date2"]))->format('Y-m-d'); //validate format
    // prepare -> bind -> execute
    $stmt = $query = $db->prepare("SELECT 1 FROM mytable WHERE date = ? ");
    $stmt->bind_param('s', $date);
    $stmt->execute();
    // get a single column from the first row of the mysql result
    $exists = (bool)$stmt->get_result()->fetch_row();

    // send JSON response to AJAX
    echo json_encode(['exists' => $exists]);
}