![]() |
How to get the text value from form dropdown - 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 get the text value from form dropdown (/showthread.php?tid=51118) |
How to get the text value from form dropdown - El Forum - 04-21-2012 [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> <?php echo form_dropdown('location', $location_enums, 'UK'); ?> </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. How to get the text value from form dropdown - El Forum - 04-21-2012 [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. How to get the text value from form dropdown - El Forum - 04-23-2012 [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 Code: //model How to get the text value from form dropdown - El Forum - 04-23-2012 [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 |