从 POST 获取复选框数组值 [英] getting a checkbox array value from POST

查看:33
本文介绍了从 POST 获取复选框数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发布一系列复选框.我无法让它工作.我没有在 foreach 循环中包含正确的语法以保持简单.但它正在工作.我通过尝试使用文本字段而不是复选框执行相同的操作来进行测试,并且它与文本字段一起使用.

i am posting an array of checkboxes. and i cant get it to work. i didnt include the proper syntax in the foreach loop to keep it simple. but it is working. i tested in by trying to do the same thing with a text field instead of a checkbox and it worked with the textfield.

<form method="post">
<?php 
foreach{
echo'
<input id="'.$userid.'" value="'.$userid.'"  name="invite[]" type="checkbox">
<input type="submit">';
}
?>
</form>

这是不工作的部分.它正在呼应邀请"而不是数组.

here is the part that is not working. it is echoing 'invite' instead of array.

<?php
    if(isset($_POST['invite'])){
$invite = $_POST['invite'];
echo $invite;
}

推荐答案

你的 $_POST 数组包含了邀请数组,所以读出它为

Your $_POST array contains the invite array, so reading it out as

<?php
if(isset($_POST['invite'])){
  $invite = $_POST['invite'];
  echo $invite;
}
?>

无法工作,因为它是一个数组.您必须遍历数组才能获取所有值.

won't work since it's an array. You have to loop through the array to get all of the values.

<?php
if(isset($_POST['invite'])){
  if (is_array($_POST['invite'])) {
    foreach($_POST['invite'] as $value){
      echo $value;
    }
  } else {
    $value = $_POST['invite'];
    echo $value;
  }
}
?>

这篇关于从 POST 获取复选框数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆