2013-03-19 70 views
1

我已经编写了一个窗体,当一个按钮被点击时添加输入元素。如何知道提交表单中输入标签的数量?

<?php 
$i=0; 
$maxid = isset($_POST['max_id'])?$_POST['max_id']+1:0; 
print '<form action="search.php" method="post" ><input type="hidden" name="max_id" value="' . $maxid . '" /><input name="ad_field_button" type="submit" value="Add Field" /></form>'; 
print '<form action="results1.php" method="post" >'; 
print '<table border="0">'; 
for($i=0;$i<=$maxid;$i++) 
    { 
     // code for adding input elements; 
    } 
print '</table>'; 
print '<input name="ad_s_button" type="submit" value="Search" />'; 
print '</form></p>'; 
?> 

我想知道单击ad_s_button按钮时提交表单中输入元素的数量。或者当ad_s_button被点击时,我怎样才能将'max_id'值传递给下一页。有什么建议么?

+0

你可以使用count($ _ POST)'count()'在'$ _POST'内保存的项目数。 – 2013-03-19 10:21:13

回答

1

或如何能通过“max_id”值到下一个页面被点击ad_s_button 时。

您可以在表单中使用隐藏字段并在其中存储max_id。

<input type="hidden" name="max_id" value="<?php echo $maxid; ?>"> 
2

如何:

print '<input type="hidden" name="Inputelements" value="'.$maxid.'">'; 

所以你必须与价值的隐藏字段ü要

+0

是的,我希望这个值与代码@Xavjer中的第二个表单一起传递 – 2013-03-19 10:23:18

0

$_POST contanis所有提交的元素。

在您的案例中($ _ POST)-1会给您输入元素的数量。 -1监守有一个按钮(提交BUTTOM)也将在那里在$ _ POST

0

您也可以发布数组。例如:

<input type='text' name='request[name]' /> 
<input type='text' name='request[surname]' /> 
<input type='text' name='request[city]' /> 

和PHP部分:

$request = $_POST['request']; 
echo '<pre>' 
echo var_dump($request); 
echo '</pre>' 

会产生

array(3){ 
    'name' => 'abc', 
    'surname' => 'bcd', 
    'city' => 'Somewhere' 
} 

然后你可以处理表单数据很容易,也容易计数输入字段,使用count($request);