Select box value

Discussion in 'PHP' started by baris22, Jan 21, 2008.

  1. #1
    Hello,

    I have got a select box for a search page and value is coming from database. When i submit the query the value for the select box resets itself to alfabetic order for the next search.

    What can i do to keep the value which was choosen.

    
    <select name="genre">
    	<?=$cats_form?> 
      </select>
    
    
    Code (markup):
    Thanks
     
    baris22, Jan 21, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Hello,

    You will need to set the SELECTED property of the <option> tag that you require to be selected.

    Jay
     
    jayshah, Jan 21, 2008 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    I do not think i can do that, because values are coming from database.

     
    baris22, Jan 21, 2008 IP
  4. Krnl

    Krnl Peon

    Messages:
    60
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #4
    
    if (isset($valuefromdb)) {
      echo "<option selected value=\"$valuefromdb\">";
      echo "<your other options>";
    } else {
      echo "<all of your options>";
    }
    
    Code (markup):
     
    Krnl, Jan 21, 2008 IP
  5. webexpert

    webexpert Banned

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you can try something like this;

    $cats_form = str_replace('<option value="'.$_REQUEST['genre'].'">', '<option value="'.$_REQUEST['genre'].'" selected>', $cats_form);

    <select name="genre">
    <?=$cats_form?>
    </select>

    This should work... however, you will need to replace this $_REQUEST['genre'] with some other variable or may be need to use this as it is, it depends on your data flow..
     
    webexpert, Jan 21, 2008 IP
  6. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Waww, thank you very much. it worked perfect..



     
    baris22, Jan 21, 2008 IP