tags:

views:

17

answers:

1

Ok I have the following function that creates a select menu so a user can choose a state.

function state_selection()
{
    $str = '<select name="state">';
    $states = array(
            'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'District of Columbia', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
        );
    foreach ($states as $state_name)
    {
        $str .= '<option value="'.$state_name.'">'.$state_name.'</option>';
    }
    $str .= '</select>';

    return $str;
}

My question is... This works great if there is just one state selection on that page... but in my form I need the user to select several states, sometimes as many as 12. How can I change the function to be able to reuse it several times within the same form... and still have it send each selection as a seperate variable.

A: 

Give your <select> elements unique names:

function state_selection($num) {
    $str = '<select name="state'.$num.'">';

Example:

<form name="form1" method="POST" action="take-form.php">
    Select State: <?php state_selection('1'); ?>
    <input type="submit" value="Submit">
</form>
jnpcl
wouldn't that require I have like 15 copies of that function in the code though?
Josepth Vodary
nm, that was a stupid question
Josepth Vodary
as in, a separate function call for each instance of the `<select>`? yes, but there's ways around that.
jnpcl