dtwr2012 2017-02-22 16:18
浏览 89
已采纳

从HTML表单传递的值不正确

The undernoted form gives a series of options to choose using a select form. When the values are retrieved from the $_POST array although the correct id is being passed the value for season simply seems to be the last value in the form.

I have checked my code and league is not being updated anywhere else as far as I can see.

I'm sure I'm missing something obvious but can't figure it out!

<form method = 'post' action = 'index.php?choice=matcht&stage=1'><table width = '50%' border = '1'><thead><tr>
<th>Select</th>
<th>League</th>
<th>Season</th>
</tr></thead>
<tr><td><input type = 'radio' name = 'id' value = '1' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21'  readonly></td>
<td><input type = 'text' name = 'season' value = '12'  readonly></td>
</tr>
<tr><td><input type = 'radio' name = 'id' value = '25' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21'  readonly></td>
<td><input type = 'text' name = 'season' value = '13'  readonly></td>
</tr>
<tr><td><input type = 'radio' name = 'id' value = '49' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21'  readonly></td>
<td><input type = 'text' name = 'season' value = '14'  readonly></td>
</tr>
<tr><td><input type = 'radio' name = 'id' value = '73' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21'  readonly></td>
<td><input type = 'text' name = 'season' value = '15'  readonly></td>
</tr>
<tr><td><input type = 'radio' name = 'id' value = '97' ></td>
<td><input type = 'text' name = 'league' value = 'MLB21'  readonly></td>
<td><input type = 'text' name = 'season' value = '16'  readonly></td>
</tr>
<tr><td colspan = '3' align = 'center'><input type = 'submit' name = 'go' value = 'GO'></td></tr>
</table>
</form>

Here is the PHP generating the HTML:

  $sql='SELECT  `TeamID`, `League`,`Season` FROM teams WHERE `FranchiseID` = 0 GROUP BY `League`,`Season` ORDER BY `League` ASC ,`Season` ASC ';
  $unmatched = $db2->get_results($sql);

    if ($db2->num_rows>0) {

  echo "<form method = 'post' action = 'index.php?choice=matcht&stage=1'>";
  echo "<table width = '50%' border = '1'>";
  echo "<thead><tr>
";
    echo "<th>Select</th>
";
    echo "<th>League</th>
";
    echo "<th>Season</th>
";
  echo "</tr></thead>
";
  foreach ( $unmatched as $unmatch )
  {
              // Access data using object syntax
              echo "<tr>";
                echo "<td><input type = 'radio' name = 'id' value = '$unmatch->TeamID' ></td>
";
                echo "<td><input type = 'text' name = 'league' value = '".$unmatch->League."'  readonly></td>
";
                echo "<td><input type = 'text' name = 'season' value = '".$unmatch->Season."'  ></td>
";
              echo "</tr>
";
  }

  echo "<tr><td colspan = '3' align = 'center'><input type = 'submit' name = 'go' value = 'GO'></td></tr>
";
  echo "</table>
";
  echo "</form>
";
  • 写回答

1条回答 默认 最新

  • dongqin1075 2017-02-22 16:21
    关注

    All of the text inputs have the same name and the last is overwriting the others. Use an array and set the index to the id so you know which ones to get:

    <tr><td><input type = 'radio' name = 'id' value = '1' ></td>
    <td><input type = 'text' name = 'league[1]' value = 'MLB21'  readonly></td>
    <td><input type = 'text' name = 'season[1]' value = '12'  readonly></td>
    

    Then you can get it like:

    $id = $_POST['id'];
    $league = $_POST['league'][$id];
    $season = $_POST['season'][$id];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?