Question

I have a form that is nothing more than a select list that is displaying records from a database and a button. All I want to do is when they select an option and click Submit, it takes them to a page that deletes the record in question. To delete it - I need the tech_id (from the select list) to be appended to the URL. I've done it the way I would with a text field, but that didn't work. Any suggestions?

<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>
Was it helpful?

Solution

The error lies in the coding construct.

I have slightly changed the position of a code segment following while statement as follows(i.e. placed it before the do phrase):

<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>

It should append the information(list-value) from the form to the URL as you desired. Check it out.

As you say it's not working there is another way using 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top