Welcome Guest, Not a member yet? Register   Sign In
How to get the text value from form dropdown
#1

[eluser]Unknown[/eluser]
Hello,

I am brand spankin' new to PHP/CI and I am struggling to get the value from a dropdown box in a form. I can get a numeric value but I need the text value so that I can save it to my database which expects a text enum.

I pass this array to my view
$data['location_enums'] = array('UK', 'US', 'Brazil', 'India', 'China');

In my view I display the dropdown box with...
<p>
<label for="location">Location:</label>
&lt;?php echo form_dropdown('location', $location_enums, 'UK'); ?&gt;
</p>

My controller calls add_user() in my model if the entire form passes validation. The add_user() method is as follows...

public function add_user($location, $role)
{

$data=array (
'name'=>$this->input->post('username'),
'password'=> md5($this->input->post('password')),
'location'=> $this->input->post('location'),
'role'=> $this->input->post('role'),
'email'=>$this->input->post('email')
);
$this->db->insert('Users',$data);

From what I can gather $this->input->post('location') returns a numeric value and not the text value that the user has selected but it's the text value I need. Any ideas?

Thank you.
#2

[eluser]Unknown[/eluser]
Ok it turns out to get the text value I put the array into another php file (and assigned it to $global_data[‘location_enums’]) and included that file in the model and controller so I could use the variable. I then altered the array in the model function add_user to the following.

'location'=> $global_data['location_enums'][$this->input->post('location')],

This returned the text value I needed to save into my database.
#3

[eluser]boltsabre[/eluser]
If you're only using that array in that one controller/model, it's a bit of an overkill to put it into a seperate php file/config/global setting, you can just do this!

Code:
//controller
$data[‘location_enums’] = array(‘UK’, ‘US’, ‘Brazil’, ‘India’, ‘China’);

//once form has passed validation
$local =  $data[‘location_enums’][$this->input->post(‘location’)];

// make your model call and pass $local to add_user($local)

Code:
//model
public function add_user($local)
  {
      
      $data=array (
          ‘name’=>$this->input->post(‘username’),
          ‘password’=> md5($this->input->post(‘password’)),
          ‘location’=> $local,
          ‘role’=> $this->input->post(‘role’),
          ‘email’=>$this->input->post(‘email’)
      );
      $this->db->insert(‘Users’,$data);
#4

[eluser]astroanu[/eluser]
Make the array like this so each value has a key

Code:
$country_list = array("LK"=>"Sri Lanka","GB"=>"United Kingdom",...... and so on






Theme © iAndrew 2016 - Forum software by © MyBB