Tyler Craft
Tyler Craft

Reputation: 174

Codeigniter update form not retrieving dropdown values

I'm fairly new to CI. I have a customer database which stores a bunch of customer information. I have also created an Update controller to update current customer information. The update form is the same form as the new customer form but for the value I've got it pulling the old data from the database. My problem is it pulls all the data and displays it in it's propor field except the drop down field. Any ideas how to fix this?

CONTROLLER:

function edit_customer($id){
    $data['success']=0;
    if($_POST){
        $data_customer=array(
            'first_name'=>$_POST['first_name'],
            'last_name'=>$_POST['last_name'],
            'phone'=>$_POST['phone'],
            'email'=>$_POST['email'],
            'website'=>$_POST['website'],
            'business_name'=>$_POST['business_name'],
            'business_add'=>$_POST['business_add'],
            'business_cityState'=>$_POST['business_cityState'],
            'cc_type'=>$_POST['cc_type'],
            'cc_number'=>$_POST['cc_number'],
            'cc_exp'=>$_POST['cc_exp'],
            'cc_cvd'=>$_POST['cc_cvd'],
            'billing_add'=>$_POST['billing_add'],
            'billing_zip'=>$_POST['billing_zip'],
            'package'=>$_POST['package'],
            'assigned_zip_code'=>$_POST['assigned_zip_code'],
            'active'=>1
        );
        $data_customer['active'] = 1;
        $this->customer->update_customer($id,$data_customer);
        $data['success']=1;
    }
    $data['customer']=$this->customer->get_customer($id);

    $this->load->view('header');
    $this->load->view('edit_customer',$data);
    $this->load->view('footer');

}

MODEL:

function update_customer($id, $data_customer){
    $this->db->where('id', $id);
    $this->db->update('customers', $data_customer);
}

VIEW DROPDOWN:

<label for="cc_type">Credit Card Type:</label>
                <select name="cc_type" value="<?=$customer['cc_type'] ?>">
                  <option></option>
                  <option>Visa</option>
                  <option>Mastercard</option>
                  <option>American Express</option>
                  <option>Discover</option>
                </select>

Upvotes: 0

Views: 2912

Answers (1)

Akhilesh B Chandran
Akhilesh B Chandran

Reputation: 6608

For an option to be selected, you need to add the selected attribute to the <option> elements.

For example:

<select name="type">
  <option>a</option>
  <option>b</option>
  <option selected="selected">c</option>
  <option>d</option>
</select>​

View it here: http://jsfiddle.net/3M4xv/

So in your code, you could do something like this:

<select name="cc_type">
  <option <?php echo ($customer['cc_type']=='Visa')?'selected="selected"':''; ?>>Visa</option>
  <option <?php echo ($customer['cc_type']=='Mastercard')?'selected="selected"':''; ?>>Mastercard</option>
  <option <?php echo ($customer['cc_type']=='American Express')?'selected="selected"':''; ?>>American Express</option>
  <option <?php echo ($customer['cc_type']=='Discover')?'selected="selected"':''; ?>>Discover</option>
</select>

Hope it helps :)

Upvotes: 1

Related Questions