Welcome Guest, Not a member yet? Register   Sign In
Codeigniter Form Ajax
#1

[eluser]ninjayan[/eluser]
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
Code:
[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
Code:
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
Code:
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!
#2

[eluser]CroNiX[/eluser]
Code:
redirect('controller/method');
it automatically adds base_url(), so you are doing it twice.
#3

[eluser]ninjayan[/eluser]
Thank you for the reply.
Another question. How can I pass a variable from model to controller? thanks
#4

[eluser]spfortier[/eluser]
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:

Code:
private $test_var;

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

Then in your controller:

Code:
$test_var = $this->user_model->getTestVar();
#5

[eluser]ninjayan[/eluser]
Oh. Thank you.
#6

[eluser]ninjayan[/eluser]
by the way, why do i need to use private in
Code:
private $test_var;
#7

[eluser]spfortier[/eluser]
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.

Code:
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();




Theme © iAndrew 2016 - Forum software by © MyBB