2009-12-17 43 views
1

是否有可能將表單字段值獲取到數組中? EX:獲取表單值到php中的數組中

<?php 

    array('one', 'two', 'three');  
    ?> 

    <form method="post" action="test.php"> 
     <input type="hidden" name="test1" value="one" /> 
     <input type="hidden" name="test2" value="two" /> 
     <input type="hidden" name="test3" value="three" /> 
     <input type="submit" value="Test Me" /> 
    </form> 

所以有可能將所有的表單值,無論它們的數量傳遞給PHP中的數組?

回答

10

它已經完成。

看看$_POST陣列。如果你做print_r($_POST);你應該看到它是一個數組。

如果你只需要值,而不是關鍵,使用

$values = array_values($_POST); 

http://php.net/manual/en/reserved.variables.post.php

+0

很大的考驗,我怎麼能得到從擺脫提交按鈕該數組被張貼?我怎麼能添加一些元素到數組中? – 2009-12-17 20:42:34

+0

我不會添加任何東西到該陣列。至於按鈕,只需將名稱字段關閉。 – 2009-12-17 20:44:43

5

這實際上是一個PHP被設計爲工作方式,和的原因之一,它取得了巨大的市場早期通過網絡編程滲透。

將表單提交給PHP腳本時,所有表單數據都將放入可在任何時間訪問的超全局數組中。因此,例如,提交你把你的問題的形式:

<form method="post" action="test.php"> 
    <input type="hidden" name="test1" value="one" /> 
    <input type="hidden" name="test2" value="two" /> 
    <input type="hidden" name="test3" value="three" /> 
    <input type="submit" value="Test Me" /> 
</form> 

將意味着裏面test.php,你將有一個名爲$_POST超全局,如果你曾與表單數據創建它會被預填充,基本上是作爲如下:

$_POST = array('test1'=>'one','test2'=>'two','test3'=>'three'); 

有POST和GET請求,即超球。 $_POST$_GET。有一個cookie數據,$_COOKIE。還有$_REQUEST,其中包含三者的組合。

查看doc page on Superglobals瞭解更多信息。

10

是的,只是名稱每一個後同樣的事情,發生支架輸入:

<form method="post" action="test.php"> 
     <input type="hidden" name="test[]" value="one" /> 
     <input type="hidden" name="test[]" value="two" /> 
     <input type="hidden" name="test[]" value="three" /> 
     <input type="submit" value="Test Me" /> 
</form> 

然後你就可以用

<?php 
print_r($_POST['test']); 
?>