suttercain Posted September 28, 2007 Share Posted September 28, 2007 Hi guys, I built a small admin panel that allows the user to submit data, via a form. One field is a select menu (html populated). Now when they try to edit the record the form is displayed with the content already filled in to allow editing. But the select menu always goes to the first select. Is there a way to have the select menu already selected to the value from the MySQL row? EXP Column State will always be CA, AZ or NV. If the record has a value of NV, how do I have that show in the select menu when the user goes to edit the record, with also allowing them to change it to either CA or AZ? Thanks. SC Link to comment https://forums.phpfreaks.com/topic/70979-solved-editing-via-a-form-populated-from-mysql-how-do-i-have-the-select-menu-selected/ Share on other sites More sharing options...
MadTechie Posted September 28, 2007 Share Posted September 28, 2007 use the selected tag <select name="test"> <option value="1">test1</option> <option value="2" selected>test2</option> <option value="3">test3</option> </select> Link to comment https://forums.phpfreaks.com/topic/70979-solved-editing-via-a-form-populated-from-mysql-how-do-i-have-the-select-menu-selected/#findComment-356892 Share on other sites More sharing options...
suttercain Posted September 28, 2007 Author Share Posted September 28, 2007 Bit I would have to do that with an if or switch statement right? What because that solution, as far as I can see, will only work if the value from the MySQL table is test 2. What if it is test 3? SC Link to comment https://forums.phpfreaks.com/topic/70979-solved-editing-via-a-form-populated-from-mysql-how-do-i-have-the-select-menu-selected/#findComment-356893 Share on other sites More sharing options...
MadTechie Posted September 28, 2007 Share Posted September 28, 2007 basic concept <?php $TheList = array("test1" => 1, "test2" => 2, "test3" => 3) echo "<select name='test'>"; foreach($TheList as $K => $V) { $sel = ($V == $_POST['test'])?"selected":""; echo "<option value='$V' $sel>$K</option>"; } echo "</select>"; ?> Link to comment https://forums.phpfreaks.com/topic/70979-solved-editing-via-a-form-populated-from-mysql-how-do-i-have-the-select-menu-selected/#findComment-356895 Share on other sites More sharing options...
suttercain Posted September 28, 2007 Author Share Posted September 28, 2007 Nice, works great. Saving this for the archive. SC Link to comment https://forums.phpfreaks.com/topic/70979-solved-editing-via-a-form-populated-from-mysql-how-do-i-have-the-select-menu-selected/#findComment-356903 Share on other sites More sharing options...
MadTechie Posted September 28, 2007 Share Posted September 28, 2007 cool Link to comment https://forums.phpfreaks.com/topic/70979-solved-editing-via-a-form-populated-from-mysql-how-do-i-have-the-select-menu-selected/#findComment-356905 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.