jaymc Posted July 31, 2008 Share Posted July 31, 2008 I am populating the first option of a select box using mysql/PHP so that the select automatically has what ever they have stored in my database <select> <option>$gender</option> <option>Female</option> <option>Male</option> </select> Works fine, however, when parsed it actually looks like this <select> <option>Male</option> <option>Female</option> <option>Male</option> </select> I know I can use <option SELECTED>Male</option> but that method requires me to generate the entire select OPTIONS via PHP, looping through each to see if it matches what is in the database. Some selects have 90 options, so I dont want to do this Is there a way I can do something like <select selectedIndexName='Male'> <option>Female</option> <option>Male</option> </select> I know thats not the syntax, but thats how I would like it to work if possible Quote Link to comment https://forums.phpfreaks.com/topic/117568-solved-select-gt-option/ Share on other sites More sharing options...
obsidian Posted July 31, 2008 Share Posted July 31, 2008 The easiest way to accomplish this is to dynamically build all your options instead of hand coding all 90 options on the big ones. Take, for instance, this example: <?php $myVal = isset($_POST['val']) ? $_POST['val'] : NULL; $vals = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five'); echo "<select name=\"val\">\n"; foreach ($vals as $k => $v) { echo "<option value=\"$k\""; echo $myVal == $k ? ' selected="selected"' : ''; echo ">$v</option>\n"; } echo "</select>\n"; ?> That's really all there is to it. Quote Link to comment https://forums.phpfreaks.com/topic/117568-solved-select-gt-option/#findComment-604687 Share on other sites More sharing options...
jaymc Posted July 31, 2008 Author Share Posted July 31, 2008 Yes, I know of that method but as per my example I want it outside of PHP I have 28 select boxes so dont want to be putting them all in loops Hence why I would like a way in javascript (reliable) or HTML to do it Quote Link to comment https://forums.phpfreaks.com/topic/117568-solved-select-gt-option/#findComment-604699 Share on other sites More sharing options...
obsidian Posted July 31, 2008 Share Posted July 31, 2008 Yes, I know of that method but as per my example I want it outside of PHP I have 28 select boxes so dont want to be putting them all in loops Hence why I would like a way in javascript (reliable) or HTML to do it There is no way in HTML to do it, and the only way in JavaScript would be to assign a JavaScript variable with PHP that holds the currently selected value so that you could "select" it using a JavaScript function. My only question is, why would you go to so much trouble to do that when you can build them all with PHP and have valid markup that way? I guess I just don't understand your aversion to using PHP for something like this. Quote Link to comment https://forums.phpfreaks.com/topic/117568-solved-select-gt-option/#findComment-604711 Share on other sites More sharing options...
jaymc Posted July 31, 2008 Author Share Posted July 31, 2008 Its a lot of code to prevent 1 duplicate option in a select your code x 28 Quote Link to comment https://forums.phpfreaks.com/topic/117568-solved-select-gt-option/#findComment-604714 Share on other sites More sharing options...
obsidian Posted July 31, 2008 Share Posted July 31, 2008 Its a lot of code to prevent 1 duplicate option in a select your code x 28 If you build all your selects as a multi-dimensional array, it's only the array assignment that will really take any time at all, and it would be more like my code x2 in size to build them all... plus, even my code x28 is a LOT less code than manually coding the HTML for 90 options... I'm not suggesting you do something with which you are not comfortable, but I do want you to get an understanding that, sometimes it's good to make a shift and use the technologies for what they are intended, even if it means a little more work up front. For instance, if you were to add another option to one of your selects down the road, the only change to code would be adding another value to the array... just a thought. Either way you go, though, good luck! Quote Link to comment https://forums.phpfreaks.com/topic/117568-solved-select-gt-option/#findComment-604728 Share on other sites More sharing options...
jaymc Posted July 31, 2008 Author Share Posted July 31, 2008 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/117568-solved-select-gt-option/#findComment-605036 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.