EllisLab text mark
Advanced Search
     
Codeigniter Form Ajax
Posted: 03 October 2012 12:10 AM   [ Ignore ]
Avatar
Joined: 2012-10-02
71 posts

Hi everyone. I’m just new to CodeIgniter. I was following different tutorials creating a form with validation and now, I want to create a form which utilizes jQuery+Ajax. So I have this code right now.

view: login

[removed]
 
$(document).ready(function() {
  
$('#login-form').on('submit', function() {
   
$('.error-messages p').remove();
   var 
login_form = $(this);
   $.
post(
    
login_form.attr('action'),
    
login_form.serialize(),
    function(
data{
     
if(data{
      
$('.error-messages').slideDown('fast').append(data);
     
}
     
else {
      [removed]
.href "home.php";
     
}
    }
'json'
   
);
   return 
false;
  
});
 
});
 
[removed] 

I will not be posting the form, just the javascript code

controller: main/login_validation

public function login_validation() {
  $this
->load->library('form_validation');

  
$this->form_validation->set_rules('username''Username''required|trim');
  
$this->form_validation->set_rules('password''Password''required|trim');

  if (
$this->form_validation->run() === FALSE{
   $data 
validation_errors();
  
}else {
   $this
->load->model('user_model');
   
$user $this->user_model->validate();

   if (
$user{
    redirect
(base_url('site/dashboard'));
   
}else {
    
echo '<p>Incorrect username and/or password.</p>';
   
}
  }
  
echo json_encode($data);
 

model: user_model

public function validate() {
  $this
->db->where('username'$this->input->post('username'));
  
$this->db->where('password'md5($this->input->post('password')));

  
$query $this->db->get('users');

  if (
$query->num_rows() == 1{
   
foreach ($query->result() as $row{
    
if ($row->status == 'Pending'{
     redirect
(base_url('main/login_pending'));
    
}elseif ($row->status == 'Disabled'){
     redirect
(base_url('main/login_disabled'));
    
}else {
     
//SET THE SESSION
     
$user_info = array(
      
'username' => $row->username,
      
'first_name' => $row->first_name,
      
'middle_initial' => $row->middle_initial,
      
'last_name' => $row->last_name,
      
'office' => $row->office,
      
'privilege' => $row->privilege,
      
'status' => $row->status,
      
'is_logged_in' => 1
     
);
     
$this->session->set_userdata($user_info);
     return 
true;
    
}
   }
  }else {
   
return false;
  
}
 } 

——-
Now, when I click login without inputs or any of the fields the validation is ok. If the user is not found, it also returns a message and it is ok. But my problem is, when the user is validated and exist, it should redirect to redirect(base_url(‘site/dashboard’)); but its not. Please help me. Thanks!

 
Posted: 03 October 2012 12:34 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3796 posts
redirect('controller/method'); 

it automatically adds base_url(), so you are doing it twice.

 Signature 
 
Posted: 03 October 2012 12:47 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2012-10-02
71 posts

Thank you for the reply.
Another question. How can I pass a variable from model to controller? thanks

 
Posted: 03 October 2012 07:08 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-10-03
2 posts

You could create a “get” function in your model and set the private variable to whatever it is you want to pass to the controller, like this:

private $test_var;

public function 
getTestVar() {return $this->test_var;

Then in your controller:

$test_var $this->user_model->getTestVar(); 

 

 
Posted: 03 October 2012 08:44 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2012-10-02
71 posts

Oh. Thank you.

 
Posted: 03 October 2012 09:13 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2012-10-02
71 posts

by the way, why do i need to use private in

private $test_var
 
Posted: 03 October 2012 09:35 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2012-10-03
2 posts

You don’t necessarily need to use private, it just provides a level of security to keep you from accidentally overwriting the value stored in the variable as it hides the variable from anything outside the class. It also promotes a better programming style. I generally use “getters” and “setters” to pass values to and from my models.

class Test {
  
private $test_var;

  public function 
setTestVar($test_var{$this->test_var $test_var;
  
public function getTestVar() {return $this->test_var;}
}

$test 
= new Test();
$test->setTestVar('Hello world!');
echo 
$test->getTestVar();