Jump to content

Hold value of select box in form


mallen

Recommended Posts

 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']

Link to comment
https://forums.phpfreaks.com/topic/281283-hold-value-of-select-box-in-form/
Share on other sites

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;

    }  

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.