still very new to CodeIgniter and trying to do the following. I may be doing this wrong, and any direction, greatly appreciated.
I have a function that is called on submit of login form, validate_creds. If successful the form logs in, if not successful I want to set an $error_msg and reload the form. The validate_creds() appears to validate correctly but as you can see on the else statement I want to reload the index() passing the $error_msg displaying the message at the top of the form.
public function index($error_msg = '')
{
//data
$data['login'] = "Logged In";
if($error_msg !== '') {
$data['error_msg'] = $error_msg;
}
//view
$this->load->view('templates/login/login_header',$data);
$this->load->view('login/login_form', $data);
$this->load->view('templates/login/login_footer',$data);
}
public function validate_creds()
{
$this->load->model('user_model');
$query = $this->user_model->validate();
if($query)//if the users creds have been validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('gallery');
}
else
{
$error_msg = "Your username or password is not correct.";
$this->index($error_msg);
}
}
Form Message
<?php if($error_msg != ''): ?>
<div class="alert alert-error">
<?php echo $error_msg; ?>
</div>
<?php endif; ?>
What currently is happening is the $error_msg is displaying all the time.
I hope this is just how it came out with copy pasting because this code is hard to read. check out php codesniffer and as an industry standard, check out some documentation on zend about php code format.
so your logic requires the error message be injected into the index function (which I’m assuming is your action but just not labeled as so), and if it’s not injected, it doesn’t declare the error message because it’s not injected.
i would look at $this->user_model->validate(); to ensure that’s properly handling the http request (post).