$ _POST里面的for循环返回null

时间:2016-05-21 15:21:27

标签: php

我试图遍历$_POST['job-destination[$i]'],但是当我做$i = 0; var_dump($_POST['job-destination[$i]']);

时它返回null

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>

1 个答案:

答案 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++){