将选择列表值附加到URL

时间:2013-10-29 14:43:18

标签: html forms url append selectlist

我有一个表单,它只是一个显示数据库和按钮记录的选择列表。我想要做的就是当他们选择一个选项并单击“提交”时,它会将他们带到删除相关记录的页面。要删除它 - 我需要将tech_id(来自选择列表)附加到URL。我已经按照文本字段的方式完成了,但是没有用。有什么建议吗?

<form method="post" id="form1" name="form1" action="delete-tech.php?tech_id=<?php echo $_POST['technician']; ?>">
  <p>Choose Technician to Delete:
    <select name="technician" id="technician" title="technician">
      <?php
do {  
?>
      <option value="<?php echo $row_getTechs['tech_id']?>"><?php echo $row_getTechs['tech_name']?></option>
      <?php
} while ($row_getTechs = mysqli_fetch_assoc($getTechs));
  $rows = mysqli_num_rows($getTechs);
  if($rows > 0) {
      mysqli_data_seek($getTechs, 0);
      $row_getTechs = mysqli_fetch_assoc($getTechs);
  }
?>
    </select>
  </p>
  <p>
    <input name="submit" type="submit" id="submit" value="Delete Technician">
  </p>
</form>

1 个答案:

答案 0 :(得分:0)

错误在于编码结构。

我稍微改变了while语句后面的代码段的位置,如下所示(即将它放在do短语之前):

<form method="GET" id="form1" name="form1" action="delete-tech.php">
  <p>Choose Technician to Delete:
    <select name="technician" id="technician" title="technician">
    <?php
      $rows = mysqli_num_rows($getTechs);
      if($rows > 0) {
          mysqli_data_seek($getTechs, 0);
          $row_getTechs = mysqli_fetch_assoc($getTechs);
          do {  
    ?>
      <option value="<?php echo $row_getTechs['tech_id']?>"><?php echo $row_getTechs['tech_name']?></option>
    <?php
          }while ($row_getTechs = mysqli_fetch_assoc($getTechs));
      }
    ?>
    </select>
  </p>
  <p>
    <input name="submit" type="submit" id="submit" value="Delete Technician">
  </p>
</form>

它应该根据需要将form中的信息(列表值)附加到网址。看看吧。

正如你所说,它不起作用,还有另一种使用 JavaScript

的方法
<form method="post" id="form1" name="form1" action="delete-tech.php">
  <p>Choose Technician to Delete:
    <select name="technician" id="technician" title="technician">
    <?php
      $rows = mysqli_num_rows($getTechs);
      if($rows > 0) {
          mysqli_data_seek($getTechs, 0);
          $row_getTechs = mysqli_fetch_assoc($getTechs);
          do {  
    ?>
      <option value="<?php echo $row_getTechs['tech_id']?>"><?php echo $row_getTechs['tech_name']?></option>
    <?php
          }while ($row_getTechs = mysqli_fetch_assoc($getTechs));
      }
    ?>
    </select>
  </p>
  <p>
    <input name="submit" type="button" id="submit" value="Delete Technician" onclick="location.href='delete-tech.php?tech_id='+document.getElementById('technician').value">
  </p>
</form>