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中的多选择复选框_Php_Ajax - Fatal编程技术网

php中的多选择复选框

php中的多选择复选框,php,ajax,Php,Ajax,我正在制作一个网站,在其中我要使用多个复选框来增强删除功能。这是我的密码。我的问题是 1.Ajax调用不起作用。 2.如何从数据库中搜索阵列 <?php if(isset($_POST['Delete'])) { $array=$_POST['check_box']; } ?> <form method="post" id="form"> <table width="200" border="1"> <tr>

我正在制作一个网站,在其中我要使用多个复选框来增强删除功能。这是我的密码。我的问题是 1.Ajax调用不起作用。
2.如何从数据库中搜索阵列

    <?php
if(isset($_POST['Delete']))
{
    $array=$_POST['check_box'];

    }
?>
<form method="post" id="form">
<table width="200" border="1">
  <tr>
    <td>select</td>
    <td>NAme</td>
    <td>Action</td>
  </tr>

  <?php
  while($selectnumberarr=mysql_fetch_array($selectnumber))
  {
  ?>
  <tr>

    <td><input type="checkbox" name="check_box[]" class="check_box" id="<?php $selectnumberarr[0]; ?>" /> </td>
    <td><?php echo $selectnumberarr[1]; ?></td>

  </tr>
  <?php
  }?>
  <input type="submit" name="Delete" id="delete">


</table>
</form>
下面是我的ajax和javascript代码

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#delete').click(function() {

        $.ajax({
            type: "POST",
            url: "checkbox.php",
            data: $('#form').serialize(),
            cache: false,
            success: function(html)
            {
                alert("true");
            }
        });//end ajax
    });
}); 
 </script>

如果您不使用数组发送复选框,则会收到任何帮助,如:

HTML部分:

<?php
if (isset($_POST['check_box'])) {
    var_dump($_POST['check_box']);
    echo "ajax call is working";
}
?>
<form id="form">
    <table width="200" border="1">
        <tr>
            <td>select</td>
            <td>NAme</td>
            <td>Action</td>
        </tr>

        <?php
        while ($selectnumberarr = mysql_fetch_array($selectnumber)) {
            ?>
            <tr>
                <td><input type="checkbox" name="check_box[]" class="check_box" value="<?php echo $selectnumberarr[0]; ?>" /> </td>
                <td><?php echo $selectnumberarr[1]; ?></td>

            </tr>
            <?php
        }
        ?>
    </table>
    <input type="button"name="delete" id="delete" value="Delete" />
</form>
这看起来像:

array(
"1", "2","etc.."
);

因此,此数组包含要删除的选定复选框的所有ID。

您的代码几乎正确。您需要删除“onChange=show for input”复选框,因为如果您有jquery,则不需要在HTML元素上放置事件

使用jquery方法与最新的php库兼容

将jquery代码替换为以下jquery代码:-

<script>
$(document).ready(function(){
  $('#delete').on('click',function()
  {

       var cat_id = $('.check_box:checked').map(function() {
           return this.id;
         }).get().join(',');
                 console.log(cat_id);

      $.ajax({
          type: "POST",
          url: "checkbox.php",
          data: { "kw ":cat_id },
          datatype:'json',
          success: function(html)
          {
              alert("true");
          }
      });//end ajax

  });
});
</script>
在jquery中使用.check_框而不是元素,以防止检查所有复选框,而不是所需的复选框


希望有帮助。

控制台中有任何错误。?如何从数据库中搜索阵列-请explain@rookieB:在上面的ajax代码中,我现在以数组形式发送数据,以便删除我必须根据该数组搜索数据库的数据。数组中的元素将获得删除形式database@Deepu:不,亲爱的,没有错误。我已经更新了我的代码,所以你可以用这个替换你的代码。那应该行得通你能解释一下吗?我已经更新了我的答案。希望这更清楚我的意思您是否将表单标记放在代码中的表标记之外,并检查了ajax请求发送到checkbox.php的数据?当我向cat_id发出警报时,,,当我在警报框中选中4个checbox时。我正在使用警报,如下所示:functionhtml{alertcat_id;}
array(
"1", "2","etc.."
);
<script>
$(document).ready(function(){
  $('#delete').on('click',function()
  {

       var cat_id = $('.check_box:checked').map(function() {
           return this.id;
         }).get().join(',');
                 console.log(cat_id);

      $.ajax({
          type: "POST",
          url: "checkbox.php",
          data: { "kw ":cat_id },
          datatype:'json',
          success: function(html)
          {
              alert("true");
          }
      });//end ajax

  });
});
</script>