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
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;

    }  
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.