I was wondering why my checkbox fails even after it has already been ticked. It is a checkbox for terms and conditions
Controller:
// Load the registration page
public function registration_page(){
$this->load->view('includes/main_index');
$this->load->view('recycler/forms/register');
$this->load->view('includes/footer');
}
public function accept_terms_conditions()
{
// Checkbox name is 'agree' which checks if the value of $checked
// is equal to 1
$checked = $this->input->post('agree');
return (int) $checked == 1 ? TRUE : FALSE;
}
$this->form_validation->set_rules('agree', '', 'callback_accept_terms_conditions');
if($this->form_validation->run() == FALSE)
{
// After form submission, it always goes here
$this->registration_page();
}
else{ /* Proceed to data insertion */ }
I printed out the post value of it using print_r function and it returned 1, yet the validation still fails. $this->form_validation->run() == FALSE means that if the form validation has errors, it will go back to the register view, which is the $this->register() method call.
Does anyone know why it keeps failing?
EDIT: Added the method for loading the registration view which is the register() method
CodePudding user response:
A checkbox brings "on" as value when it is checked not 1.
public function accept_terms_conditions()
{
// returns true if the checkbox 'agree' has been checked
return ($this->input->post('agree') === 'on');
}
And your condition is wrong, you are saying that if the validation has failed then call method register I think it should be the other way round.
CodePudding user response:
try
public function accept_terms_conditions() {
$checked = ($this->input->post('agree')) ? TRUE : FALSE;
return $checked;
}