Hi, I am trying to create a login form with ajax. I got the code from the external sources but it is always incorrect whenever I try to login.
here’s the code:
<script>
$(document).ready(function (){
$("#userForm").submit(function (e){
e.preventDefault();
var url = $(this).attr('action');
var method = $(this).attr('method');
var data = $("#userForm").serialize();
$.ajax({
url: 'http://ospar.hostzi.com/authorized/login',
type: "post",
data:data
}).done(function(data){
if(data !=='')
{
$("#response").show('fast');
$("#response").effect( "shake" );
$('#userForm')[0].reset();
}
else
{
window.location.href='http://ospar.hostzi.com/authorized/dashboard';
throw new Error('go');
}
});
});
});
</script>
<div id="formLog" style="width: 450px; margin: 0 auto; background-color: #efefef; padding: 30px; border-radius: 8px;">
<form id="userForm" role="form" action="http://ospar.hostzi.com/authorized/login" method="POST">
<p id="status"></p>
<p class="alert alert-danger" id="response"><b>INVALID USER NAME OR PASSWORD</b></p>
<div class="form-group">
<label for="exampleInputEmail1">Username</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="Email" name="username" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" name="password" />
</div>
<button type="submit" class="btn btn-default" value="Submit">Submit</button>
</form>
</div>
Controller:
public function login(){
$this->load->library('form_validation');
$this->load->model('Patient_history');
$username = $this->input->post('username');
$password = $this->input->post('password');
//call the model for auth
if($this->Patient_history->login($username, $password)){
redirect('authorized/dashboard');
}
else{
echo'something went wrong';
}
}
model:
public function login($username, $password){
$this->db->where('firstname',$username);
$this->db->where('lastname',$password);
$query = $this->db->get('patient_account');
if($query->num_rows()==1){
foreach ($query->result() as $row){
$data = array(
'firstname'=> $row->username,
'logged_in'=>TRUE
);
}
$this->session->set_userdata($data);
return TRUE;
}
else{
return FALSE;
}
}
please tell me what am I doing wrong.