frist44 Posted March 5, 2009 Share Posted March 5, 2009 I'm trying to use a form to submit info and have the state of those select boxes remember what was selected. This is my line, but it doesn't like it: <option value="asaexton" <?php echo if($_POST['ddsite']=="asaexton")?"selected":""; ?> >Exton</option> Link to comment https://forums.phpfreaks.com/topic/148115-drop-down-state/ Share on other sites More sharing options...
cooldude832 Posted March 5, 2009 Share Posted March 5, 2009 best method of reproducing select values <?php $select = array("Option1","Option2","Option3","Option4","Option5); echo "<select name='test'>"; foreach($select as $key=>$value){ echo "<option value='".$key."' "; if($_POST['select'] == $key){ echo 'selected = 'selected' "; } echo >".$value."</option>"; } echo "</select>"; ?> super easy and easy to reproduce Link to comment https://forums.phpfreaks.com/topic/148115-drop-down-state/#findComment-777495 Share on other sites More sharing options...
kickstart Posted March 5, 2009 Share Posted March 5, 2009 Hi Try:- <option value="asaexton" <?php echo (($_POST['ddsite']=="asaexton") ? "selected='selected'":""); ?> >Exton</option> All the best Keith Link to comment https://forums.phpfreaks.com/topic/148115-drop-down-state/#findComment-777505 Share on other sites More sharing options...
frist44 Posted March 5, 2009 Author Share Posted March 5, 2009 best method of reproducing select values <?php $select = array("Option1","Option2","Option3","Option4","Option5); echo "<select name='test'>"; foreach($select as $key=>$value){ echo "<option value='".$key."' "; if($_POST['select'] == $key){ echo 'selected = 'selected' "; } echo >".$value."</option>"; } echo "</select>"; ?> super easy and easy to reproduce I have this and it works when I postback, but not on initial load: <select name="ddsite"> <?php $select = array('asasungard' => 'Sungard', 'asaexton' => 'Exton'); foreach($select as $key=>$value){ echo '<option value="'.$key.'"'; if($_POST['ddsite'] == $key){ echo 'selected="selected" '; } echo ">".$value."</option>"; } ?> </select> Do I need something to see if that post value is set? Link to comment https://forums.phpfreaks.com/topic/148115-drop-down-state/#findComment-777544 Share on other sites More sharing options...
cooldude832 Posted March 5, 2009 Share Posted March 5, 2009 well of course on initial load $_POST is unset to "trick" around this sort your array so the answer you want to be set before POST is set set to be the array's first node i.e $select[0]; Link to comment https://forums.phpfreaks.com/topic/148115-drop-down-state/#findComment-777545 Share on other sites More sharing options...
frist44 Posted March 5, 2009 Author Share Posted March 5, 2009 I ended up using this and just test the variable extract($_POST, EXTR_SKIP); if (!$_POST){ $ddpriority = "%"; $dddate = date("Y-m-d"); $ddsite = "asasungard"; } Link to comment https://forums.phpfreaks.com/topic/148115-drop-down-state/#findComment-777546 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.