I am using codeigniter, i have written a function to check if a user password exists which it does. This is my model
-
The model: user
public function get_some_password($username,$password) {
$this->db->where('user_password', $password); $this->db->where('user_username',$username); $query=$this->db->get('some_users_table'); if($query->num_rows()==1){ return true; }else{ return false; } -
the controller
public function check_password() {
$username=$this->uri->segment(3);
$temp_pass= $this->input->post(‘current_password’);
$password=md5($temp_pass);
$this->user->get_some_password($username,$password);}- The ajax on the view
//done on page load
var success1 = $(“.success”); //a div on the view that appears if success
var error1 = $(“.error”); //a div on the view that appears if error
success1.hide();
error1.hide();$('#change_password').click(function() { var username = $('#username').val(); dataString2 = $('#changpassword').serialize(); $.ajax({ type: "POST", url: '<?php echo base_url(); ?>controller_name/check_password/' + username, data: dataString2, success: function() { $('.success').html('password successfully updated!'), success1.slideDown('slow'); }, error: function() { $('.error').html('Wrong current password!'), error1.slideDown('slow'); } });
The problem: Ajax loads the success div even when the username or password returned is false, where am i missing something
This is a correct behavior as jquery error is executed when response code is not 200:
1) You can parse returned value in success method.
e.g.
2) You can return error code from server 404, 500, 503 … To trigger execution of error function.
e.g.
note: Header should executed before any output is done.
Try in your controller: