g_pmattoo Posted March 18, 2008 Share Posted March 18, 2008 Here is what i tried but it is not working. Any clue why or anyone has any code snippets to pass across. Also the fields in this form are transported to an xml and i get the index value(0,1,2 or 3) of selected option in test dropdown rather than it's value(No,abc,xyz or efg) $option = array(0 => 'No', 1 => 'abc', 2 => 'xyz', 3 => 'efg'); $selected = (isset($_POST['test']) && intval($_POST['test']) < 4) ? $_POST['test'] : ''; //index of dropdown echo "<select name=\"test\">\n"; echo createOptionFromArray($option,$selected); echo "</select>\n"; function createOptionFromArray($myArray,$selected) { if(!is_array($myArray)) { return false; } $returned = $select = ''; foreach($myArray as $key => $value) { if($selected == $key) { $select = ' selected'; } $returned .= "<option value=\"$key\"$select>$value</option>"; $select = ''; } return $returned; } Link to comment https://forums.phpfreaks.com/topic/96797-issues-with-remembering-the-selected-option-of-dropdown-in-php/ Share on other sites More sharing options...
frijole Posted March 18, 2008 Share Posted March 18, 2008 $option = array(0 => 'No', 1 => 'abc', 2 => 'xyz', 3 => 'efg'); $selected = (isset($_POST['test']) && intval($_POST['test']) < 4) ? $_POST['test'] : ''; //index of dropdown echo "<select name=\"test\">\n"; echo createOptionFromArray($option,$selected); echo "</select>\n"; function createOptionFromArray($myArray,$selected) { if(!is_array($myArray)) { return false; } $returned = $select = ''; foreach($myArray as $key => $value) { if($selected == $key) { $select = ' selected'; } $returned .= "<option value=\"$key\"$select>$value</option>"; $select = ''; } return $returned; } Link to comment https://forums.phpfreaks.com/topic/96797-issues-with-remembering-the-selected-option-of-dropdown-in-php/#findComment-495346 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.