Arnold Porche Villaluz
Arnold Porche Villaluz

Reputation: 213

get id from drop-down select tag to put into link

I have a drop-down select tag for Patients:

<select>
<?php
$qPatient = mysql_query("SELECT idpatients, firstName, mi, lastName, suffix FROM patients ORDER BY lastName ASC");  

while($rowPatient = mysql_fetch_array( $qPatient )) {
if(isset($rowPatient['suffix']) && !empty($rowPatient['suffix'])){$suffix = " " . $rowPatient['suffix'];}else{$suffix = NULL;}
            if(isset($rowPatient['mi']) && !empty($rowPatient['mi'])){$mi = " " . $rowPatient['mi'] . ".";}else{$mi = NULL;}
            echo "<option value=" . $rowPatient['idpatients'] . $rowPatient . ">" . $rowPatient['lastName'] . $suffix . ", " . $rowPatient['firstName'] . $mi . "</option>";
        }
    ?>
</select>

This generates a list of patients from the drop-down. How do I get the patientID based on the selection from my drop-down to put to my link?

ex: <a href="update.php?patientID=$idpatients">Update</a>

where $idpatients = the patient ID from the drop-down list select tag.

Also, I need to have 3 different links: 1.) update.php 2.) chart.php and 3.) report.php

Upvotes: 0

Views: 1073

Answers (1)

Piotr M&#252;ller
Piotr M&#252;ller

Reputation: 5558

Set this form method to GET. After submiting, value of select field will be shown as get variable. So, add name to your select:

<select name="parientId">

And proper action to your form:

<form action="update.php" method="GET">

And, of course submit button.

This is "static way" with form". If you are sure that you want to have link, not submit button, you can do it with jQuery:

$("#go").click(function(){ $("#idOfYourForm").submit(); }
...
<a href="" id="go">Go !</a>

Or catch .change() (sorry, not select()) on your <select...> and set $("#go").attr('href','update.php?patientId='+$("#yourSelectId").val());

P.S. Get from SQL only fields you need, not *

Update

<form...>
<select name="patientId" id="patientSelect">
...
</select>
<a id="updatelink" href="">....</a>
<a id="deletelink" href="">....</a>
<script  type="text/javascript">
$(document).ready(function(){
  $("#patientSelect").change(function(){
    $("#updatelink").attr('href',"update.php?id="+$("#parientSelect").val());
    $("#deletelink").attr('href',"delete.php?id="+$("#parientSelect").val());
  }
});
</script>

Something like that, written fast, not checked/tested

Upvotes: 1

Related Questions