我试图遍历$_POST['job-destination[$i]']
,但是当我做$i = 0; var_dump($_POST['job-destination[$i]']);
PHP代码
// Experience loop
$experience = '';
for ($i=0; $i <= 10; $i++){
$experience .="<h4>Experience $i: </h4>
<b>Destination</b>: ".$_POST['job-destination[$i]']."<br/>";
}
HTML代码
<div class="form-group">
<p>Destination</p>
<div class="radio-btn-grp">
<input class="job-destination" name="job-destination[0]" value="Australia" type="radio" required="">
<label for="job-destination">Australia</label>
</div>
<div class="radio-btn-grp">
<input class="job-destination" name="job-destination[0]" value="Canada" type="radio">
<label for="job-destination">Canada</label>
</div>
</div>
<div class="form-group">
<p>Destination</p>
<div class="radio-btn-grp">
<input class="job-destination" name="job-destination[1]" value="Australia" type="radio" required="">
<label for="job-destination">Australia</label>
</div>
<div class="radio-btn-grp">
<input class="job-destination" name="job-destination[1]" value="Canada" type="radio">
<label for="job-destination">Canada</label>
</div>
</div>
答案 0 :(得分:1)
<强> PHP 强>
替换它:
$_POST['job-destination[$i]']
用这个:
$_POST['job-destination'][$i]
在您的HTML输入中,使用[]
之类的内容会自动为您的输入编制索引:
<强> HTML 强>
<div class="form-group">
<p>Destination</p>
<div class="radio-btn-grp">
<input class="job-destination" name="job-destination[]" value="Australia" type="radio" required="">
<label for="job-destination">Australia</label>
</div>
<div class="radio-btn-grp">
<input class="job-destination" name="job-destination[]" value="Canada" type="radio">
<label for="job-destination">Canada</label>
</div>
</div>
您还应该更改for循环以迭代$_POST['job-destination']
<强> PHP 强>
for ($i=0; $i <= count($_POST['job-destination']); $i++){