Does anyone know how to set the default value (displayed value) of a drop down menu to that of a value from a database? I've tried the following, but "DD" is always displayed as the default value: <select name = "select_day" value = <?php echo $select_day; ?> /> <option value = "DD">DD</option> <option value = "01">01</option> <option value = "02">02</option> <option value = "03">03</option> <option value = "04">04</option> <option value = "05">05</option> <option value = "06">06</option> <option value = "07">07</option> <option value = "08">08</option> <option value = "09">09</option> <option value = "10">10</option> <option value = "11">11</option> <option value = "12">12</option> <option value = "13">13</option> <option value = "14">14</option> <option value = "15">15</option> <option value = "16">16</option> <option value = "17">17</option> <option value = "18">18</option> <option value = "19">19</option> <option value = "20">20</option> <option value = "21">21</option> <option value = "22">22</option> <option value = "23">23</option> <option value = "24">24</option> <option value = "25">25</option> <option value = "26">26</option> <option value = "27">27</option> <option value = "28">28</option> <option value = "29">29</option> <option value = "30">30</option> <option value = "31">31</option> HTML: Any ideas? Regards
The value that should be selected and displayed is contained within the variable $select_day That why I've set the value to this, but it doesn't seem to work.
Hmm, ofcourse DD is the value now, since the first option contains this value.. So either make the first option having the value of the variable $select_day, or... well, that's actually the only possibility to do this I think. So for example: <select name = "select_day" /> <option value = "<?php echo $select_day; ?>"> Select </option> <option value = "DD">DD</option> <option value = "01">01</option> <option value = "02">02</option> <option value = "03">03</option> <option value = "04">04</option> <option value = "05">05</option> <option value = "06">06</option> <option value = "07">07</option> <option value = "08">08</option> <option value = "09">09</option> <option value = "10">10</option> <option value = "11">11</option> <option value = "12">12</option> <option value = "13">13</option> <option value = "14">14</option> <option value = "15">15</option> <option value = "16">16</option> <option value = "17">17</option> <option value = "18">18</option> <option value = "19">19</option> <option value = "20">20</option> <option value = "21">21</option> <option value = "22">22</option> <option value = "23">23</option> <option value = "24">24</option> <option value = "25">25</option> <option value = "26">26</option> <option value = "27">27</option> <option value = "28">28</option> <option value = "29">29</option> <option value = "30">30</option> <option value = "31">31</option> </select> Code (markup): for example.
There are two methods for achieving this. 1. The method that skyfe suggested. In this method, you place the database value as the first option in the select menu. This way, your selected value will appear twice: once in the top and another time in its place in the menu. 2. Loop through the values inside the <select> and add "selected" before the database value. Example: <select name="select_day"> <? for($i=01;$i<32;$i++){ ?> <option value="<?=$i?>" <? if($i == $select_day) echo "checked"; ?>><?=$i?></option> <? } ?> </select> HTML:
Ah did he mean it like that, yes, then I guess your method is good indeed . So if it's equal to $select_day it does only show it on the top, and not twice ^^
Thank you It will not show it in the top, it will make it the default value (as he asked) so when you click the menu, you will find that you are on the selected 10 for example and there are from 1 to 9 above your selection.