Results 1 to 7 of 7

Thread: How to get a drop-down to retain a selection

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default How to get a drop-down to retain a selection

    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

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Can you post the code you are using?
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Code:
    <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>
    Last edited by kuau; 04-21-2010 at 06:03 PM. Reason: simplified code

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Something like this I think not exactly sure

    PHP Code:
    <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>
    Corrections to my coding/thoughts welcome.

  5. The Following User Says Thank You to bluewalrus For This Useful Post:

    kuau (04-21-2010)

  6. #5
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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
    Last edited by kuau; 04-21-2010 at 11:15 PM.

  7. #6
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    I wouldn't put this on a live site...

    PHP Code:
    <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>
    Corrections to my coding/thoughts welcome.

  8. The Following User Says Thank You to bluewalrus For This Useful Post:

    kuau (04-22-2010)

  9. #7
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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!
    Last edited by kuau; 04-22-2010 at 01:08 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •