CodeIgniter Forums
How to select value in dropdown from query? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to select value in dropdown from query? (/showthread.php?tid=4537)



How to select value in dropdown from query? - El Forum - 11-30-2007

[eluser]jplanet[/eluser]
I am sure many of you have successfully accomplished this:

I have a simple form where customers can edit their address. I can query the database for the customer address and populate all of the fields with:

Code:
$this->load->model('customer_model');

//return row with address data
$billing = $this->customer_model->getBilling($this->session->userdata('customer_id'));

////set validation rules etc.

$this->validation->set_fields($fields);
        
        $this->validation->bill_first_name = $billing->first_name;
        $this->validation->bill_last_name = $billing->last_name;
        $this->validation->bill_company_name = $billing->company_name;
        $this->validation->bill_address = $billing->address;
        $this->validation->bill_city = $billing->city;

This all works beautifully. But, when I get to the state dropdown, no luck:

Quote:$this->validation->bill_state = $billing->state;

There is no state selected even through $billing->state returns a value that has a match in the form.

My state dropdown looks like this (abbreviated):

Code:
<select name="bill_state" id="bill_state">

<option value="" selected>Select a State</option>
<option value="AL" &lt;?= $this->validation->set_select('bill_state', 'AL'); ?&gt;>Alabama</option>
<option value="AK"  &lt;?= $this->validation->set_select('bill_state', 'AK'); ?&gt;>Alaska</option>
<option value="AZ" &lt;?= $this->validation->set_select('bill_state', 'AZ'); ?&gt;>Arizona</option>
<option value="AR" &lt;?= $this->validation->set_select('bill_state', 'AR'); ?&gt;>Arkansas</option>
<option value="CA" &lt;?= $this->validation->set_select('bill_state', 'CA'); ?&gt;>California</option>
<option value="CO" &lt;?= $this->validation->set_select('bill_state', 'CO'); ?&gt;>Colorado</option>
<option value="CT" &lt;?= $this->validation->set_select('bill_state', 'CT'); ?&gt;>Connecticut</option>
<option value="DE" &lt;?= $this->validation->set_select('bill_state', 'DE'); ?&gt;>Delaware</option>

etc...

Any suggestions would be appreciated!


How to select value in dropdown from query? - El Forum - 12-03-2007

[eluser]jabbett[/eluser]
Your first option ("Select a State") has its "selected" attribute set-- that'll always be selected by default even if some other option is chosen. Try removing it; remember, if no option has its "selected" attribute set, the first item in your list will automatically be selected, so you don't need to do it explicitly.

Can you post the HTML that's being generated?


How to select value in dropdown from query? - El Forum - 12-03-2007

[eluser]Armchair Samurai[/eluser]
I usually use the form helper - works like a charm:
Code:
form_dropdown('foo', $bar, $this->validation->foo);
Using $this->validation->set_select() for pre-population has never worked for me.


How to select value in dropdown from query? - El Forum - 12-03-2007

[eluser]Unknown[/eluser]
Yep, I just tested it,
Code:
form_dropdown('foo', $bar, $this->validation->foo);
works like a charm.