2013-05-29 79 views
2

當我嘗試提交這個簡單的測試形式,以PHP:PHP提交陣列POST失敗

<form action="test.php" method="post"> 
<input name ="lang_learn[0]lang" type="text" value="1"><br> 
<input name ="lang_learn[0]level" type="text" value="2"><br> 
<input name ="lang_learn[1]lang" type="text" value="3"><br> 
<input name ="lang_learn[1]level" type="text" value="4"><br> 
<input type="submit"> 
</form> 

我期望在$ _POST數組是這樣的:

Array 
(
[lang_learn] => Array 
    (
     [0] => Array ([lang] => 1, [level] => 2) 
     [1] => Array ([lang] => 3, [level] => 4) 
    ) 

) 

,而不是我得到這個:

Array 
(
[lang_learn] => Array 
    (
     [0] => 1 
     [1] => 4 
    ) 

) 

我試着用不同的服務器上的不同安裝,我總是得到相同的結果。 問題在哪裏?閱讀這個應該是正確的做法。

回答

5

輸入字段的名稱需要修復:

<input name ="lang_learn[0][lang]" type="text" value="1"><br> 
<input name ="lang_learn[0][level]" type="text" value="2"><br> 
<input name ="lang_learn[1][lang]" type="text" value="3"><br> 
<input name ="lang_learn[1][level]" type="text" value="4"><br> 
+0

該做的伎倆,謝謝 – Hulud

3

試試這個,

<form action="test.php" method="post"> 
<input name ="lang_learn[0][lang]" type="text" value="1"><br> 
<input name ="lang_learn[0][level]" type="text" value="2"><br> 
<input name ="lang_learn[1][lang]" type="text" value="3"><br> 
<input name ="lang_learn[1][level]" type="text" value="4"><br> 
<input type="submit"> 
</form> 
5

您需要使用子陣列,就像你在PHP會。每個密鑰應該用[]包圍。試試這個...

<form action="test.php" method="post"> 
<input name ="lang_learn[0][lang]" type="text" value="1"><br> 
<input name ="lang_learn[0][level]" type="text" value="2"><br> 
<input name ="lang_learn[1][lang]" type="text" value="3"><br> 
<input name ="lang_learn[1][level]" type="text" value="4"><br> 
<input type="submit"> 
</form> 
2

你可能需要這樣做:

<form action="test.php" method="post"> 
<input name ="lang_learn[0][lang]" type="text" value="1"><br> 
<input name ="lang_learn[0][level]" type="text" value="2"><br> 
<input name ="lang_learn[1][lang]" type="text" value="3"><br> 
<input name ="lang_learn[1][level]" type="text" value="4"><br> 
<input type="submit"> 
</form> 
0

你的語法是不正確的:name ="lang_learn[0]lang"必須name ="lang_learn[0][lang]"