Valrus Posted March 30, 2009 Share Posted March 30, 2009 Hi, I'm looking for a method in which I can set the selected option of drop-down select box (a <Select><Option> group) through scripting. I don't see any way to do it, maybe I'm just missing something. What I'm trying to accomplish is I'm pulling a bunch of data from a database and then populating a page for editing. Which is relatively easy except part of the data is represented by option groups in select boxes which I would need to set based on the value of the pulled data. Thanks for any help you can give me. Link to comment https://forums.phpfreaks.com/topic/151794-setting-select-boxes-after-creation/ Share on other sites More sharing options...
savagenoob Posted March 30, 2009 Share Posted March 30, 2009 Here is a script I use, hopefully this is what you are talking about... <?php //Database query and putting data into an array $query = mysql_query("SELECT * FROM whatever"); while ($dbarray = mysql_fetch_assoc($query)) { $whatever[] = $dbarray['whatever']; } ?> //then put this in your html form to dispay the data for Select boxes <?php echo '<SELECT name=whatever>'; foreach ($whatever as $key => $value) { echo '<OPTION value=' . $value . '> ' . $value . ''; } echo '</select>'; ?> Link to comment https://forums.phpfreaks.com/topic/151794-setting-select-boxes-after-creation/#findComment-797042 Share on other sites More sharing options...
lonewolf217 Posted March 30, 2009 Share Posted March 30, 2009 here is a basic example. here i am comparing the array value to a database entry. you can also compare to a $_POST['name'] form entry <?php $array = ('Tom','Jack','John','Steve'); echo "<SELECT name=\"test\">"; $row = mysql_fetch_array($query) foreach($array as $name) { if($name == $row['name']) { echo "<OPTION selected=\"selected\">".$name."</OPTION>"; } else { echo "<OPTION>".$name."<OPTION>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/151794-setting-select-boxes-after-creation/#findComment-797046 Share on other sites More sharing options...
Valrus Posted March 30, 2009 Author Share Posted March 30, 2009 Thank you, but that's not quite what I'm hoping to achieve. Perhaps I can illustrate what I'd like to do. <HTML> <?php selectionFunction() { Some code here to select the proper selection option. } ?> <select style="formSelect" name="ranking" id="ranking"> <optgroup label="Ranking" /> <option value="" /> <OPTION VALUE="1">1 - First Choice</OPTION> <OPTION VALUE="2">2</OPTION> <OPTION VALUE="3">3</OPTION> <OPTION VALUE="4">4</OPTION> <OPTION VALUE="5">5</OPTION> <OPTION VALUE="6">6</OPTION> <OPTION VALUE="7">7</OPTION> <OPTION VALUE="8">8</OPTION> <OPTION VALUE="9">9</OPTION> <OPTION VALUE="10">10</OPTION> <OPTION VALUE="-1">Banned</OPTION> </select> <?php selectionFunction(); ?> </HTML> Obviously stripped down a bit, but hopefully it illustrates what I want. The reason I want to do this is so I can re-use a whole bunch of code for add/edit/view pages for this data. Otherwise I'm gonna have to re-do a whole bunch of stuff. Link to comment https://forums.phpfreaks.com/topic/151794-setting-select-boxes-after-creation/#findComment-797084 Share on other sites More sharing options...
lonewolf217 Posted March 30, 2009 Share Posted March 30, 2009 no, it doesn't really say what you want. all you have to do is add "selected=selected" inside the option tag of the one you want highlighted. how you choose which one this is , is up to you Link to comment https://forums.phpfreaks.com/topic/151794-setting-select-boxes-after-creation/#findComment-797092 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.