views:

144

answers:

6

i have several HTML Select dropdowns on the same page, used in slightly different contexts. they both use the same option data that is stored and echoed out of a php variable that was built from a database query.

The Problem i am running into is that one works and returns the selected value, while the other one only returns the value of the first option.

this is my markup for the one that works

                    <select name="Home_State" id="Home_State">
                <?php echo $states;?>
                </select>

the options for the one that works

<option value="1">AL</option><option value="2">AK</option><option value="4">AZ</option><option value="5">AR</option><option value="6">CA</option><option value="8">CO</option><option value="9">CT</option><option value="10">DE</option><option value="11">DC</option><option value="12">FL</option><option value="13">GA</option><option value="15">HI</option><option value="16">ID</option><option value="17">IL</option><option value="18">IN</option><option value="19">IA</option><option value="20">KS</option><option value="21">KY</option><option value="22">LA</option><option value="23">ME</option><option value="24">MD</option><option value="25">MA</option><option value="26">MI</option><option value="27">MN</option><option value="28">MS</option><option value="29">MO</option><option value="30">MT</option><option value="31">NE</option><option value="32">NV</option><option value="33">NH</option><option value="34">NJ</option><option value="35">NM</option><option value="36">NY</option><option value="37">NC</option><option value="38">ND</option><option value="39">OH</option><option value="40">OK</option><option value="41">OR</option><option value="42">PA</option><option value="44">RI</option><option value="45">SC</option><option value="46">SD</option><option value="47">TN</option><option value="48">TX</option><option value="49">UT</option><option value="50">VT</option><option value="51">VA</option><option value="53">WA</option><option value="54">WV</option><option value="55">WI</option><option value="56">WY</option>

This is my markup for the one that does not work

                        <select name="Mail_State" id="Mail_State">
                         <?php echo $states;?>
                    </select>

and the options for the one that does not work

<option value="1">AL</option><option value="2">AK</option><option value="4">AZ</option><option value="5">AR</option><option value="6">CA</option><option value="8">CO</option><option value="9">CT</option><option value="10">DE</option><option value="11">DC</option><option value="12">FL</option><option value="13">GA</option><option value="15">HI</option><option value="16">ID</option><option value="17">IL</option><option value="18">IN</option><option value="19">IA</option><option value="20">KS</option><option value="21">KY</option><option value="22">LA</option><option value="23">ME</option><option value="24">MD</option><option value="25">MA</option><option value="26">MI</option><option value="27">MN</option><option value="28">MS</option><option value="29">MO</option><option value="30">MT</option><option value="31">NE</option><option value="32">NV</option><option value="33">NH</option><option value="34">NJ</option><option value="35">NM</option><option value="36">NY</option><option value="37">NC</option><option value="38">ND</option><option value="39">OH</option><option value="40">OK</option><option value="41">OR</option><option value="42">PA</option><option value="44">RI</option><option value="45">SC</option><option value="46">SD</option><option value="47">TN</option><option value="48">TX</option><option value="49">UT</option><option value="50">VT</option><option value="51">VA</option><option value="53">WA</option><option value="54">WV</option><option value="55">WI</option><option value="56">WY</option>

and then getting the form value using CI

 $Mail_State = $this->input->post('Mail_State');
 echo $Mail_State

returns the value of the first option regardless of what option is selected.

+2  A: 

Ah, I have just managed to decode what you are actually asking - as the question was quite unclear.

By first option, you mean the first block of HTML (which has been repeated, with a different name/id later in the page), and not the first <option> tag.

I've actually written a test program though, with and without the trailing /, and with default options (like <option selected="selected" value="4">AZ</option>), and I still can't get it to only return the choice from the first select tag. When I print_r($_POST), they return separate IDs.

Therefore, I believe your problem may lay elsewhere.


How do I get all the results from a select multiple HTML tag?

<select name="var[]" multiple="yes">
.....
</select>
Alister Bulman
please dont hijack my question
Eric
Hijack? What do you mean?
Darryl Hein
A: 

If the trailing / on the first select fixes it, do that in the short term until you can figure out what the heck is going on. Focus on solving the mystery after you aren't stuck. Also, fur a multiselect, I believe your name attribute should have [] at the end to indicate it will be holding an array of values i.e. name="Mail_State[]"

Zak
i had not noticed that i had a / in the opening tag, i removed it and the one that works still works
Eric
A: 

A few things:

  • Your first selects includes a / at the end of the opening tag. This will effect how the data is submitted.
  • Can you post the final HTML of the page (the output of the PHP). What you have there isn't much help as we don't know what the options are that are in the select. Also include the form table.
  • Check for any other fields with the same name.
  • Try doing print_r($_REQUEST) at the top of the page you are submitting to to see what the values are. (It's possible the class you are using doesn't support multiples as well.)
Darryl Hein
A: 

I did not full understood the error, but seems like you are using CodeIgniter, why not utilise its Form Helper that may make your code easier and perhaps solve the problem as well.

Try this in your controller:

$Home_State = $this->input->post('Home_State');
$Mail_State = $this->input->post('Mail_State');

And in your view:

echo form_dropdown('Home_state', $states, $Home_State);
echo form_dropdown('Mail_State', $states, $Mail_State);
rockacola
A: 

Could there be other form inputs that also have the same name in the form? It happens to me sometimes. I forget that I haven't changed the other form names after copy and pasting multiple form inputs.

Thorpe Obazee
A: 

Is it possible that your other dropdowns are declared outside of the form?

espais