我有两个这样的表格:
表格1:
<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'>
表格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'>
车辆输入是由JavaScript添加的动态输入,那么如何识别正确人所拥有的车辆?我已经检索了数据,但我不知道每个人如何拆分车辆。
答案 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
}
}
答案 1 :(得分:0)
您可以为每个附加表单创建不带name[]
和vehicles[]
名称的输入,但将输入包装在用户(或您想要的任何其他名称)数组中,并执行users[].name[]
之类的操作和users[].vehicles[]
。(我不确定正确的语法,但事情是包装它)