2017-07-29 105 views
0

我有2種形式是這樣的:具有相同名稱的陣列中的多個形式

表一:

<input type="text" name='name[]' placeholder = 'Input your name' > 
<input type="text" name='vehicles[]' placeholder = 'Input your first vehicle'> 
<input type="text" name='vehicles[]' placeholder = 'Input your second vehicle'> 
.... 
<input type="text" name='vehicles[]' placeholder = 'Input your xxx vehicle'> 

表二:

<input type="text" name='name[]' placeholder = 'Input your name' > 
<input type="text" name='vehicles[]' placeholder = 'Input your first vehicle'> 
<input type="text" name='vehicles[]' placeholder = 'Input your second vehicle'> 
.... 
<input type="text" name='vehicles[]' placeholder = 'Input your xxx vehicle'> 

所述的車輛輸入是動態的輸入通過JavaScript添加,那麼如何識別正確的人擁有的車輛?我已經檢索了數據,但我不知道如何按每個人分割車輛。

+0

你需要給每個輸入字段不同的名稱,以保存所有輸入值。 –

+0

因此每個表單都有不同的名稱? –

+0

所有的輸入都是單一形式或多種形式? –

回答

0

我已經爲您的車輛領域實施了一個解決方案,希望您能夠修改此代碼以滿足您的需求。

在名稱末尾添加了一些在創建它使用JavaScript,即:

var counter = 0; 
function addInput() { 
    document.getElementById("form").insertAdjacentHTML("beforeend", "<input type=\"text\" name=\"vehicles"+counter+"\" placeholder=\"Input vehicle #"+counter+"\">"); 
    counter++; 
} 

然後,在PHP中,你可以簡單地得到通過$_POST發送的所有項目的列表:

$vehicles = array_keys($_POST); //Get all of the $_POST values 
for ($i=0;$i<count($vehicles);$i++) { //Loop through all $_POST values 
    if (strpos($vehicles[$i], "vehicles")!==false) { //If the name contains "vehicles" 
     $value = $_POST[$vehicles[$i]]; //Get the value of the field 
     echo $i . " " . $value . "<br>"; //Echo it out as a test 
    } 
} 
0

您可以創建一個name[]vehicles[]名稱爲每個附加形式不是投入,而是在用戶輸入包裹(或任何其他你想要的名字)陣列,並完成類似users[].name[]users[].vehicles[]。(我不肯定是正確的語法,但事情是包裝它)

相關問題