mallen Posted August 17, 2013 Share Posted August 17, 2013 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; } $option = array(0 => 'Rent', 1 => 'Own'); $selected = (isset($_POST['rent_own']) && intval($_POST['rent_own']) < 2) ? $_POST['rent_own'] : ''; print "<select name=\"rent_own\">\n"; print createOptionFromArray($option,$selected); print "</select>\n"; I am trying to get my form to hold the value of the selext box which it does. But the value I get back is "1" or "0". I need to have the value be "Rent" or "Own". How can I do this? $rent_own = $_POST['rent_own'] Quote Link to comment https://forums.phpfreaks.com/topic/281283-hold-value-of-select-box-in-form/ Share on other sites More sharing options...
mallen Posted August 18, 2013 Author Share Posted August 18, 2013 Fixed it by changing $key to $value function createOptionFromArray($myArray,$selected) { if(!is_array($myArray)) { return false; } $returned = $select = ''; foreach($myArray as $key => $value) { if($selected == $value) { $select = ' selected'; } $returned .= "<option value=\"$value\"$select>$value</option>"; $select = ''; } return $returned; } Quote Link to comment https://forums.phpfreaks.com/topic/281283-hold-value-of-select-box-in-form/#findComment-1445708 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.