正从POST一个复选框数组值 [英] getting a checkbox array value from POST

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

问题描述

我张贴复选框的数组。 ,我不能让它的工作。我没有包含在foreach循环的正确语法保持简单。但它的工作。我测试了通过尝试做同样的事情用一个文本字段,而不是一个复选框,并将它与文本框的工作。

 <形式方法=邮报>
< PHP
{的foreach
回声'
<输入ID ='。$用户ID'值='。$用户ID。NAME =邀请[]类型=复选框>
<输入类型=提交>';
}
?>
< /表及GT;

在这里是行不通的部分。这是呼应邀请,而不是数组。

 < PHP
    如果(使用isset($ _ POST ['邀请'])){
$邀请= $ _ POST ['邀请'];
回声$邀请;
}


解决方案

您$ _POST数组包含邀请数组,所以读它作为

 < PHP
如果(使用isset($ _ POST ['邀请'])){
  $邀请= $ _ POST ['邀请'];
  回声$邀请;
}
?>

将无法工作,因为它是一个数组。您可以通过数组必须循环得到所有的值。

 < PHP
如果(使用isset($ _ POST ['邀请'])){
  如果(is_array($ _ POST ['邀请'])){
    的foreach($ _ POST ['邀请']为$值){
      回声$价值;
    }
  }其他{
    $值= $ _ POST ['邀请'];
    回声$价值;
  }
}
?>

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;
}

解决方案

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天全站免登陆