Log in

View Full Version : How to get a drop-down to retain a selection



kuau
04-21-2010, 04:49 PM
I have 2 tables. One holds the clientID and first & lastname. The other holds the clientID and notes related to the client. I managed to create a dropdown list of names from which the viewer can choose and as soon as a selection is made, the form executes (without a Submit button) and shows the notes below.

The problem is that the drop-down immediately reverts to the first option in the list (Select an existing client...) as soon as it shows the notes, such that the client name is no longer visible. Is there a way to make the drop down stay on the name that was selected until another is selected? Or is there a better way to do this? Thanks, e :)

bluewalrus
04-21-2010, 05:02 PM
Can you post the code you are using?

kuau
04-21-2010, 05:09 PM
<form method="GET" name="SelectClient" action="client-history.php">
<label>Client Name:</label>
<select name="client_id" onChange="document.SelectClient.submit()">
<option value="0">Select an existing client...</option>
<?php $sql = "SELECT client_id, lastname, firstname FROM client ORDER BY lastname, firstname ASC ";
$result = mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
while($row = mysql_fetch_row($result)){ ?>
<option value="<?php echo $row[0];?>"><?php echo $row[1]; echo ', '; echo $row[2];?></option>
<?php } ?>
</select>
</form>

bluewalrus
04-21-2010, 07:42 PM
Something like this I think not exactly sure


<form method="GET" name="SelectClient" action="client-history.php">
<label>Client Name:</label>
<select name="client_id" onChange="document.SelectClient.submit()">
<?php
if (isset($_GET['client_id']) && $_GET['client_id'] != "0" && $_GET['client_id'] != "") {
?>
<option value="<?php echo $_GET['client_id'];?>" default><?php echo $_GET['client_id'];?></option>
<?php
} else {
?>
<option value="0">Select an existing client...</option>
<?php }
$sql = "SELECT client_id, lastname, firstname FROM client ORDER BY lastname, firstname ASC ";
$result = mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
while($row = mysql_fetch_row($result)){ ?>
<option value="<?php echo $row[0];?>"><?php echo $row[1]; echo ', '; echo $row[2];?></option>
<?php } ?>
</select>
</form>

kuau
04-21-2010, 09:45 PM
Dear BW: That worked!! (sort of) It did retain the clientID in the drop-down but what I need in the drop-down is the client name instead. This is probably really simple but I am too tired to think straight and can't figure out how to get the name to display instead of the ID number. I tried doing another SELECT clause but ended up with nothing but commas. They can't use this form if they cannot see the client name. Please help me finish it off. Thanks for your help. e :)

bluewalrus
04-21-2010, 11:33 PM
I wouldn't put this on a live site...


<form method="GET" name="SelectClient" action="client-history.php">
<label>Client Name:</label>
<select name="client_id" onChange="document.SelectClient.submit()">
<?php
if (isset($_GET['client_id']) && $_GET['client_id'] != "0" && $_GET['client_id'] != "") {
$ID_IS = $_GET['client_id'];
//prevent sql injection only allow numbers
settype($ID_IS, "integer")
$sql2 = "SELECT lastname, firstname FROM client where client_id = $ID_IS";
$result2 = mysql_query($sql2,$connection) or die("Couldn't execute $sql2 query.");
// Not familiar with mysql this might not be right
$row2 = mysql_fetch_row($result2)
$user_is = $row2[0];
$user_is =. $row2[1];
?>
<option value="<?php echo $ID_IS;?>" default><?php echo $user_is;?></option>
<?php
} else {
?>
<option value="0">Select an existing client...</option>
<?php }
$sql = "SELECT client_id, lastname, firstname FROM client ORDER BY lastname, firstname ASC ";
$result = mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
while($row = mysql_fetch_row($result)){ ?>
<option value="<?php echo $row[0];?>"><?php echo $row[1]; echo ', '; echo $row[2];?></option>
<?php } ?>
</select>
</form>

kuau
04-22-2010, 12:25 AM
Dear BW:

I tried your code and it was my fault it didn't work. I had a more complicated query that joined to another table to get the zip code, so when I removed that and used the exact code you used it did exactly what I was after. Thank you so much!!! You really saved the day. I really appreciate your help. Sorry I complicated it.

Mahalo plenty! :)