Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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_Arrays_Forms_Checkbox - Fatal编程技术网

表单复选框值php

表单复选框值php,php,arrays,forms,checkbox,Php,Arrays,Forms,Checkbox,我有一个由php中的while循环创建的表单,如下所示: <form action='#' method='post'> <input type='hidden' name='form_valid' id='form_valid'> <?php $i=-1; $result_array = array(); //the while is from a simple mysql query while( $line = $results->fetch() ) {

我有一个由php中的while循环创建的表单,如下所示:

<form action='#' method='post'>
<input type='hidden' name='form_valid' id='form_valid'>
<?php
$i=-1;
$result_array = array();
//the while is from a simple mysql query
while( $line = $results->fetch() )
{
  $i++;
  echo"<input type='checkbox' class='checkbox' value='".$line->nid."' id='".$i."'>";
  echo $line->title;
  echo'<br>';
  $result_array[$i] = $line->nid;
}
<input type='submit'>
?>
</form>

之后,在代码中,我只想将复选框的值存储在一个新数组中:

if (isset($_REQUEST['form_valid'])) //checking is form is submitted
{
   foreach($result_array as $result)
   {
      if($result == $_REQUEST[$j])  <<<< ERROR
      {
        $final_array[$j] = $result;
      }
   }
 }
if(isset($\u请求['form\u valid'])//检查表单是否已提交
{
foreach($result\u数组作为$result)
{

如果($result=$\u REQUEST[$j])不尝试这样做,这只会使处理变得困难,只需使用一个分组名称数组:
name=“checkboxes[]”
,然后在提交时,所有选中的值只需转到
$\u POST['checkboxes']

<!-- form -->
<form action="" method="POST">
<?php while($line = $results->fetch()): ?>
    <input type="checkbox" class="checkbox" name="nid[<?php echo $line->nid; ?>]" value="<?php echo $line->nid; ?>" /><?php echo $line->title; ?><br/>
<?php endwhile; ?>
<input type="submit" name="submit" />

您的复选框没有name属性,因此您将无法在表单submit/post上访问它们。我应该在name字段中输入什么?所有复选框或$I的名称相同?您将使用
$I
,因为您正在使用
$\u请求[$j]
->
名称=“$I”。
if(isset($_POST['submit'], $_POST['nid'])) {
    $ids = $_POST['nid']; // all the selected ids are in here
    // the rest of stuff that you want to do with it
}