Php 验证数组中发送的复选框

Php 验证数组中发送的复选框,php,Php,我有一个带有多个复选框的表单,我想将其放入一个数组中。我使用这里提供的示例: 所以我准备了文件: checkbox.php: <form action="checkboxes2.php" method="post"> <input type="checkbox" name="tags[]" value="1" />1<br> <input type="checkbox" name="tags[]" value="2" />2<br> &

我有一个带有多个复选框的表单,我想将其放入一个数组中。我使用这里提供的示例:

所以我准备了文件:

checkbox.php:

<form action="checkboxes2.php" method="post">
<input type="checkbox" name="tags[]" value="1" />1<br>
<input type="checkbox" name="tags[]" value="2" />2<br>
<input type="checkbox" name="tags[]" value="3" />3<br>
<input type="checkbox" name="tags[]" value="4" />4<br>
<input type="submit" value="Send" />
</form>
我完全不知道我做错了什么。我参考了上面链接中的示例,做了完全相同的事情(主要是复制/粘贴和更改某些部分,如添加提交按钮),但没有得到示例中所示的输出。我至少应该得到每个复选框的值,对吗

我想做的是:我想检查复选框数组,查看哪些复选框已被选中,并将“是”或“否”添加到第二个数组中,如下所示:

<?php
  $number1 = $_POST["tags"];
  $number2 = array();

  foreach($number1 as $number1_output)
  {
    if(isset($number1_output))
    {
      $number2[] = "yes";
    }
    else
    {
      $number2[] = "no";
    }
  }

  print_r($number2);
?>
$no = array(0 => "no", 1 => "no"); // or array("no", "no");
$fourthChecked = $checkValues[4] !== "no";

处理数组中的复选框并验证它们的最佳方法是什么?

未选中的复选框在表单提交时不发送任何内容。这意味着,如果未选中任何
标记
复选框,则$\u POST数组中将不会有
标记
属性

因此,您的代码应该是

<?
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   if (isset($_POST['tags'])) {
      print_r($_POST['tags']);
   }
}
?>
那就有

<?php
if (b blah blah blah verify post stuff) {
    for ($i = 1; $i <= $max_tags_allowed; $i++) {
      if (isset($_POST['tags'][$i])) {
          $message2[] = 'yes';
      } else {
          $message2[] = 'no';
      }
    }
}

首先创建一个包含所有可能键的“no”的数组,如下所示:

<?php
  $number1 = $_POST["tags"];
  $number2 = array();

  foreach($number1 as $number1_output)
  {
    if(isset($number1_output))
    {
      $number2[] = "yes";
    }
    else
    {
      $number2[] = "no";
    }
  }

  print_r($number2);
?>
$no = array(0 => "no", 1 => "no"); // or array("no", "no");
$fourthChecked = $checkValues[4] !== "no";
然后,您可以获得所需的数组,如下所示:

$checkValues = array_flip($_POST["tags"]) + $no;
这样,$_POST[“tags”]中的值将成为键,+运算符合并两个数组,保留键,如果第二个数组中的值出现在第一个数组中,也会忽略第二个数组中的值。这样,您可以像这样检查:

<?php
  $number1 = $_POST["tags"];
  $number2 = array();

  foreach($number1 as $number1_output)
  {
    if(isset($number1_output))
    {
      $number2[] = "yes";
    }
    else
    {
      $number2[] = "no";
    }
  }

  print_r($number2);
?>
$no = array(0 => "no", 1 => "no"); // or array("no", "no");
$fourthChecked = $checkValues[4] !== "no";

我认为这比其他答案简单得多。祝你好运

或者,如果希望复选框在未选中时也发送值,则可以在复选框之前添加隐藏的输入字段:

<input type="hidden" name="tag[1]" value="off" />
<input type="checkbox" name="tag[1]" value="on />


在checkbox.php中:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>My form</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" languaje="javascript">
$(document).ready(function() {
  $('.checkbox').click(function(){
    var values="";
    $('.checkbox:checked').each(function(index) {           
        values+=$(this).val();
        $('#tagsVal').val(values);
    });
  });
});
</script>
</head>
<body>
  <form action="checkboxes2.php" method="post">
   <input type="checkbox" name="tags1" value="1" class="checkbox"/>1<br><!--Notice the class="checkbox" attribute is important for the jquery function-->
   <input type="checkbox" name="tags2" value="2" class="checkbox"/>2<br>
   <input type="checkbox" name="tags3" value="3" class="checkbox"/>3<br>
   <input type="checkbox" name="tags4" value="4" class="checkbox"/>4<br>
   <input type="hidden" name="tagsVal" id="tagsVal" value="" />
   <input type="submit" value="Send" />
  </form>
</body>
</html>
问候
Luis

这是复选框的工作方式,仅当您选择它们并传递值时。(据我所知)嘎,你太快了。写了完全相同的东西。经典的解决方案是向数组中添加一个
隐藏的
输入
,使其始终存在。@Marc,似乎OP想要跟踪键,所以
$message2[$i]='yes'
$message2[$i]=“否”
@coder:这就引出了一个问题:为什么不简单地使用带有value=“yes”的
标记[1]
类型表示法。如果没有设定,那就是“不”。是的,你考虑过了,不完全确定OP到底想要什么。谢谢!这正是我想要的。现在,无论是否选中复选框,我都会得到一个值。很多tnx!想知道浏览器在这方面的兼容性以及JS中的序列化功能。。。我建议在客户端使用JS检查复选框是否选中,如果未选中,则创建一个隐藏输入(我们称之为no_tags)并使用submit函数发送它,然后PHP代码将有两个变量要检查:
var_dump($_POST['tags')
变量转储($_POST['no_tags'])浏览器兼容性不会成为问题。我只是需要一个网站,只有我使用这个。但是,如果我在一个更开放的网站上使用它(也许有一天我会这样做),我会记住你的建议。