3

我有一个带有多个复选框的表单,我想将其放入一个数组中。我通过此处提供的示例进行操作:http ://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html

所以我准备了文件:

复选框.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>

复选框2.php:

<?php
print_r($_POST["tags"]);
?>

很简单...我意识到我应该只获取这些文本框的值,而不是它们是否被选中。但我仍然收到此错误:

Undefined index: tags in checkboxes2.php on line 2

我完全不知道我在这里做错了什么。我通过上面链接中的示例进行了所有操作(主要是复制/粘贴和更改某些部分,例如添加提交按钮),但我没有得到示例中所示的输出。我至少应该得到每个复选框的值,对吧?

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

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

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

  print_r($number2);
?>

嗯......它只工作了一半。只有已选中的复选框才会添加到数组中。因此,如果我选择“3”和“4”,我会得到:

Array ( [0] => yes [1] => yes )

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

4

4 回答 4

4

Checkboxes which are not selected to do not send anything when the form submits. That means if NONE of those tags checkboxes are selected, there will be no tags attribute in the $_POST array.

As such, your code should be

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

The first line verifies that a POST has actually taken place, and the second line verifies that there's actually any data to dump out.

For the rest of your code, the isset is rather pointless... Since a checkbox is only present in the POST data if it was selected, there's no point in doing the isset() within your foreach loop - there'd be nothing to foreach on if there were no checkboxes, so by definition everything in the foreach will be isset() == true anyways.

As such, your foreach loop will only produce yes values, never a no.

A better workaround is to specify array keys within your form:

<input type="checkbox" name="tags[1]" value="1" />
<input type="checkbox" name="tags[2]" value="2" />
etc...

then have

<?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';
      }
    }
}
于 2012-10-18T14:57:51.290 回答
2

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

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

因为复选框仅在设置时才发送,但它会覆盖隐藏,因为它稍后在 HTML 中,您现在将始终tag[1]设置为“关闭”或“打开”,具体取决于该框是否被选中。

于 2012-10-18T15:06:15.367 回答
0

在您的 checkboxes.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>

因此,您将在 $_POST['tagVal'] 中获得什么,您将获得您的价值观。如果选中复选框 1,3 和 4,您将得到一个字符串 134。在您的 checkboxes2.php 中,您只需在需要时将字符串拆分为数组。

$arr1 = str_split($_POST['tagVal']);

问候路易斯

于 2012-10-18T15:42:02.200 回答
0

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

$no = array(0 => "no", 1 => "no"); // or array("no", "no");

然后你可以得到你想要的数组:

$checkValues = array_flip($_POST["tags"]) + $no;

这样,$_POST["tags"] 中的值将成为键,并且 + 运算符合并保留键的两个数组,如果它们存在于第一个数组中,也会省略第二个数组中的值。这样你就可以像这样检查:

$fourthChecked = $checkValues[4] !== "no";

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

于 2012-10-18T15:04:45.617 回答