Reputation: 395
I currently have a form which includes a drop down menu that has menu items taken from a database. What I'm looking to achieve is to allow the user to select one of these values and add it to the database.The form is currently submitting and inserting properly except for the database. When I try to add the drop down value to the database all input comes back as null. If I take it out, it works fine though.
<form name = "form1" id = "form1" method ="post"> <!--action="<?php echo base_url()."index.php/Admin/create_user"; ?>"-->
<?php echo validation_errors(); ?>
<label for="first_name" class = "labelForm">First Name:</label>
<input type="text" id="first_name" name="first_name" class = "input2">
<label for="last_name" class = "labelForm">Last Name:</label>
<input type="text" id="last_name" name="last_name" class = "input2">
<label for="username" class = "labelForm">Username:</label>
<input type="text" id="username" name="username" class = "input2" onblur="check_if_exists();">
<label for="password" class = "labelForm">Password:</label>
<input type="password" id="password" name="password" class = "input2" onblur="validatePassword();">
<label for="passconf" class = "labelForm">Password:</label>
<input type="password" id="passconf" name="passconf" class = "input2" onblur="checkPasswords();">
<label for="email" class = "labelForm">Email:</label>
<input type="text" id="email" name="email" class = "input2">
<!-- <label for="hospital" class = "labelForm">Hospital:</label>
<select name="product" class = "input2" id = "hospitals">
<option selected disabled hidden style='display: none' value=''></option>
<?php foreach($hospital_dropdown as $option){?>
<option id = "hospitals" name="hospitals" value="<?php $option->hospitalName;?>"> <?php print_r($option->hospitalName); ?> </option>
<?php }?>
</select>-->
<label for="hospitals" class = "labelForm">Hospital:</label>
<select name="product" class = "input2" id = "hospitals">
<?php foreach($hospital_dropdown as $index => $option):?>
<option id = "hospitals_<?=$index?>"
name="hospitals"
value="<?=$option->hospitalName;?>"
><?=$option->hospitalName;?></option>
<?php endforeach;?>
</select>
<button type="button" id = "new_user_submit">Add New User</button>
</form>
Controller:
function create_user(){
$this->load->model('User_model');
$password = $this->input->post('password');
$hash = $this->bcrypt->hash_password($password);
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'username' => $this->input->post('username'),
'password' => $hash,
'class' => $this->input->post('userclass'),
'hospital' => $this->input->post('hospitals'),
'email' => $this->input->post('email'),
);
$this->User_model->create_user($data);
$username = $this->session->userdata('username');
$data['hospital_dropdown'] = $this->User_model->hospital_dropdown();
$data['main_content'] = 'admin';
$this->load->view('includes/admin/template', $data);
}
Model:
function create_user($data){
$insert = $this->db->insert('users', $data);
return $insert;
}
If I take out the hospital part in the controller it submits fine but when I leave it in , all the fields come back as null and won't submit. Any Ideas? Thanks!
Upvotes: 0
Views: 5600
Reputation: 7997
You have several mistakes in your php part of the mark-up:
create a unique id: <option id = "hospitals" >
is not unique
you need to echo out the option value, right now it is empty
you use print_r()
to echo out your hospitalName. Normally, in your scenario you do that with echo()
. print_r is useful to test variable content, see more here
there are other syntax to mix php with your mark-up, CodeIgniter suggest these:
the <select></select>
tag has missing/wrong name attribute, see here. You are using the name attribute "product" instead, but in you controller you write 'hospital' => $this->input->post('hospitals')
resuming: below should fix your issue(s)
<select name="hospitals" id="hospitals">
<?php foreach($hospital_dropdown as $index => $option):?>
<option value="<?=$option->hospitalName;?>">
<?=$option->hospitalName;?>
</option>
<?php endforeach;?>
</select>
Upvotes: 1