php - CodeIgniter drop down list not populating in the correct location -
i new ci , php. have dropdown list of countries populated table in database. have managed ci grab , render data via mysql, it's populating in plain text above form, , not instead, in values of drop down list. below simplified code. how can values in drop down list?
my view...
<body> <?php echo validation_errors(); ?> <?php echo form_open('form'); ?> <h5>country</h5> <select name="countryselect"> <?php echo form_dropdown(countryselect);?> </select> <br><br> <div><input type="submit" value="submit" /></div> <?php echo form_close() ?> </body>
my controller...
<?php class form extends ci_controller { function index() { $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); $this->form_validation->set_rules('firstname', 'first name', 'trim|required|max_length[12]|xss_clean'); $this->form_validation->set_rules('lastname', 'last name', 'trim|required|max_length[12]|xss_clean'); $this->form_validation->set_rules('email', 'email', 'trim|required|valid_email'); $this->form_validation->set_rules('pledge', 'pledge', 'trim|required|max_length[12]|decimal|xss_clean'); $this->load->model('country'); $data['country'] = $this->country->get_country(); if ($this->form_validation->run() == false) { $this->load->view('myform'); } else { $this->load->database(); $sql = "insert donations (firstname, lastname, email, pledge) values ( ".$this->db->escape($this->input->post('firstname')).", ".$this->db->escape($this->input->post('lastname')).", ".$this->db->escape($this->input->post('email')).", ".$this->db->escape($this->input->post('pledge')).")"; $this->db->query($sql); echo $this->db->affected_rows(); $this->load->view('formsuccess'); } } } ?>
my model...
<?php class country extends ci_model { function get_country() { $this->load->database(); $return[''] = 'please select'; $this->db->order_by('name'); $query = $this->db->get('countries'); foreach($query->result_array() $row){ echo '<option value="'.$return[$row['id']] = $row['name'].'">',$return[$row['id']] = $row['name'],'</option>'. "\n"; } return $return; } } ?>
you code little messed up, way named country
, not countryselect
in
$data['country'] = $this->country->get_country();
so use $country
variable in view. important thing form_dropdown
required array
parameter, not option
.
have here;
if want create select in way don't need form_dropdown
regular html
:
<select name="select_country"><?php echo $country; ?></select>
and need pass view:
$this->load->view('myform', $data);
edit:
remember controller != model != view
, have use them single purpose.
you used controller saving data in db, concettually wrong, should pass data model , let perform queries...and should use activerecord instead of prevent sqlinjection
.
in module managed data, creating select, if can, better thing pass query result controller , let manage that.
Comments
Post a Comment