2013-03-19 32 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);

相關問題