Ty44ler Posted February 12, 2010 Share Posted February 12, 2010 I need to set the initial value of a dropdown box to be set to whatever is in the database. Example... Database: color="red" dropdown box has choices of yellow, red, blue. dropdown box when page is opened is initially set to red The database values will be changing though. How can I make it so that the dropdown box is initially set to whatever is in the database? Quote Link to comment https://forums.phpfreaks.com/topic/191898-setting-initial-value-dynamic-dropdown-box/ Share on other sites More sharing options...
premiso Posted February 12, 2010 Share Posted February 12, 2010 Show your current code for the dropdown and I am sure someone can easily help you. Quote Link to comment https://forums.phpfreaks.com/topic/191898-setting-initial-value-dynamic-dropdown-box/#findComment-1011472 Share on other sites More sharing options...
Ty44ler Posted February 12, 2010 Author Share Posted February 12, 2010 <select name="Building" onchange="if (valDrop(this.value)) return true; else alert('Please Select a Building.');"> <option value="">Please Select a Building</option> <option value="School">School</option> <option value="Commercial Building">Commercial Building</option> <option value="Industrial">Industrial</option> <option value="Military">Military</option> <option value="HealthCare">Health Care</option> <option value="Institutional">Institutional</option> <option value="Other Building">Other</option> </select> I have a field in my database called Building and the values assigned to it can be any of the choices. I know you have to put select="selected" in the option tag that you want to initially set, but how do you place that code within the option tags depending on the value in the database? Normally, if I'm trying to recall the data in a form I just do <input type="text" value="<? echo $building; ?>" /> but that won't work because the name is never used to set the initial value. Does this make sense? Quote Link to comment https://forums.phpfreaks.com/topic/191898-setting-initial-value-dynamic-dropdown-box/#findComment-1011505 Share on other sites More sharing options...
Ty44ler Posted February 13, 2010 Author Share Posted February 13, 2010 anyone got any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/191898-setting-initial-value-dynamic-dropdown-box/#findComment-1011871 Share on other sites More sharing options...
jl5501 Posted February 13, 2010 Share Posted February 13, 2010 ok you need to do this assuming you have your $building value <select name="Building" onchange="if (valDrop(this.value)) return true; else alert('Please Select a Building.');"> <option value="">Please Select a Building</option> <option value="School" <?php if($building == 'School') echo 'selected="selected"';?>>School</option> <option value="Commercial Building">Commercial Building</option> <option value="Industrial">Industrial</option> <option value="Military">Military</option> <option value="HealthCare">Health Care</option> <option value="Institutional">Institutional</option> <option value="Other Building">Other</option> </select> I did the first one, you need to apply the same logic to the other options Quote Link to comment https://forums.phpfreaks.com/topic/191898-setting-initial-value-dynamic-dropdown-box/#findComment-1011873 Share on other sites More sharing options...
Ty44ler Posted February 13, 2010 Author Share Posted February 13, 2010 ok you need to do this assuming you have your $building value <select name="Building" onchange="if (valDrop(this.value)) return true; else alert('Please Select a Building.');"> <option value="">Please Select a Building</option> <option value="School" <?php if($building == 'School') echo 'selected="selected"';?>>School</option> <option value="Commercial Building">Commercial Building</option> <option value="Industrial">Industrial</option> <option value="Military">Military</option> <option value="HealthCare">Health Care</option> <option value="Institutional">Institutional</option> <option value="Other Building">Other</option> </select> I did the first one, you need to apply the same logic to the other options oh wow, I can't believe I didn't even think of that. That will work. Thanks! Now, what if the dropdown box has a huge list of options? I know you could do it the same way, but I was wondering if there were an easier way to iterate through all the options and write the selected? Quote Link to comment https://forums.phpfreaks.com/topic/191898-setting-initial-value-dynamic-dropdown-box/#findComment-1011896 Share on other sites More sharing options...
premiso Posted February 13, 2010 Share Posted February 13, 2010 An example, if the values for the dropdown are not coming from a database then store them in an array: $buildings = array("School", "Commercial Building", "Industrial"); // add more. $dd = '<select name="Building" onchange="if (valDrop(this.value)) return true; else alert(\'Please Select a Building.\');">'; $dd .= '<option value="">Please Select a Building</option>'; foreach ($buildings as $build) { $dd .= '<option value="' . $build . '" ' . ($building == $build?'selected="selected"':'') . '>School</option>'; } $dd .= '</select>'; echo $dd; Should work, then you just need to add to the $buildings array to add more. Quote Link to comment https://forums.phpfreaks.com/topic/191898-setting-initial-value-dynamic-dropdown-box/#findComment-1011913 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.