我刚刚创建了一个具有相同名称的输入字段的表单。我希望在提交表单后将所有这些值存储在一个变量中。
代码是
<div class="selectContainer">
<label>Number Of Person:</label>
<select id="ddTest" name="person">
<option value="">select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</div>
<div class="hidden is1">
<a href="#" class="remove" rel="1">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is2">
<a href="#" class="remove" rel="2">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is2">
<a href="#" class="remove" rel="2">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is3">
<a href="#" class="remove" rel="3">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is3">
<a href="#" class="remove" rel="3">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is3">
<a href="#" class="remove" rel="3">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is4">
<a href="#" class="remove" rel="4">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is4">
<a href="#" class="remove" rel="4">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is4">
<a href="#" class="remove" rel="4">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is4">
<a href="#" class="remove" rel="4">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is5">
<a href="#" class="remove" rel="5">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is5">
<a href="#" class="remove" rel="5">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is5">
<a href="#" class="remove" rel="5">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is5">
<a href="#" class="remove" rel="5">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>
<div class="hidden is5">
<a href="#" class="remove" rel="5">remove</a>
<label>Name</label>
<input type="text" name="name" >
<label>Age</label>
<input type="text" name="age" >
</div>`
如何将此值存储在一个变量中。请帮助我是php的新手
答案 0 :(得分:2)
$name = $_POST['name'];
$age = $_POST['age'];
for($l=0; $l < count($name); $l++)
{ //your condition
}
不要忘记在你的HTML中添加它
<label>Name</label>
<input type="text" size="20" name="name[]" id="name" >
</td>
<td>
<label>Age</label>
<input type="text" size="20" name="age[]" id="age" >
</td>
答案 1 :(得分:1)
使用[]
为所有相似名称创建一个数组:
<input type="text" name="name[]" >
<input type="text" name="age[]" >
然后你可以迭代它们:
for($i = 0; $i <= count($_POST['name']); $i++) {
echo $_POST['name'][$i] . "<br>";
echo $_POST['age'][$i] . "<br>";
}