I've a form to add a project with a checkbox if the project is already finished or not. This is the html in the view (codeigniter):
<div class="form-group">
<label>Task completed:</label>
<input type="checkbox" class="form-control" id="task_completed" name="task_completed" />
<?php echo form_error('task_completed'); ?>
</div>
I load the data in my controller:
$checked = (int)$this->input->post('task_completed');
when i do a echo "" . $checked;
it's always 0!! Also, in my database. Where is my fault?
In the input name add []
like so
<input type="checkbox" class="form-control" id="task_completed" name="task_completed[]" />
And try $data = $this->input->post('task_completed');
then var_dump($data);
HTML
<label>Task completed:</label>
<input type="checkbox" class="form-control" id="task_completed" name="task_completed" value="1" />
And to verify if was checked, you can do something like this:
<input type="checkbox" class="form-control" id="task_completed" name="task_completed" value="1" <?php echo $settings->task_completed ? 'checked' : ''; ?>
Basically get settings in models and send to view.
Controller:
public function settings()
{
$data["settings"] = $this->admin_model->siteSettings();
$this->load->view('settings', $data);
}
Model
public function siteSettings()
{
return $this->db->get('site_settings')->row();
}
View
<input type="checkbox" class="form-control" id="task_completed" name="task_completed" value="1" <?php echo $settings->task_completed ? 'checked' : ''; ?>>