表单复选框值php
我有一个由php中的while循环创建的表单,如下所示:表单复选框值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() ) {
<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
}