SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question help filling dropdown box

    Hi Guys,

    I am still trying to get this solved, ran into another problem.
    I am trying to autofill a dropdown list with data from the DB.

    PHP Code:
    <select name="collection1" OnChange="location.href=collection.collection1.options[selectedIndex].value">
      
      <?php 
      
    if(isset($_POST['vehicle_type'])) {
         
         echo 
    "<option value=\"$_POST[vehicle_type]\">$_POST[vehicle_type]</option>";}
    else {
         echo 
    "<option value=\"\">please select</option>";
         
       }
    ?>
      
         
     <? 
     
    require_once ('../mysql_connect.php'); // Connect to the database.


    // make the query to get the vehicle types
    $query "SELECT vehicle_type FROM vehicles"


    // run the query
    $result = @mysql_query ($query); 
     
     
    while(
    $row=mysql_fetch_array($result)) 
       {

    $vehicle_type=explode(','$row['vehicle_type']); 
    for(
    $i=0;$vehicle_type[$i];$i++) 

    echo 
    "<option value=request-a-part-test3.php?vehicle_type=$vehicle_type[$i]>$vehicle_type[$i]</option>";  
    }}   

    echo 
    "</select>";  

    ?> 
        
       </select>
    The list is being correctly filled, however when the page refreshes it isnt making the item I selected in the dropdown selected?

    PHP Code:

    if(isset($_POST['vehicle_type'])) {
         
         echo 
    "<option value=\"$_POST[vehicle_type]\">$_POST[vehicle_type]</option>";} 
    Can anyone possibly tell me what I have done wrong?

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you must add selected="selected" to tell the browser to preselect a specific <option>


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
  •