jquery ajax login in codeigniter..... - El Forum - 03-28-2012
[eluser]sivams20[/eluser]
I have a simple jquery-ajax login form in codeigniter.But its not working.It not even shows error.can anyone help please.
Here is my ajax code
Code: [removed]
$(document).ready(function(){
$("#submit").click(function(){
var username=$("#usr").val();
var password=$("#pass").val();
$.ajax({
type: "POST",
url: "/site/logval",
data: ({usr: username, pass: password}),
success: function(response){
if(response=='TRUE')
{
Redirect('site/home');
}
else
{
Redirect('site/login');
}
}
});
});
});
[removed]
Here is my code for controller
Code: function login()
{
$this->load->view('login');
}
function logval()
{
$this->form_validation->set_rules('usr','Username','required');
$this->form_validation->set_rules('pass','Password','required');
if($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else
{
$data['usr'] = $this->input->post('usr');
$data['pass'] = $this->input->post('pass');
if($this->user_model->check_login($data)== TRUE)
{
//$this->load->view('home');
return TRUE;
}
else
{
//$this->load->view('failure');
return FALSE;
}
}
}
and here is my code for model
Code: function check_login($data)
{
$username=$data['usr'];
$password=$data['pass'];
$query = $this->db->select('uname, pass');
$query = $this->db->from('users');
$query = $this->db->where('uname', $username);
$query = $this->db->where('pass', $password);
// $query = $this->db->where('Password_VC', $password);
$query = $this->db->get();
if($query->num_rows() > 0)
{
return true;
//return $query->result();
}
else
{
return false;
}
}
}
jquery ajax login in codeigniter..... - El Forum - 03-28-2012
[eluser]CodeIgniteMe[/eluser]
First, you need to add the CI's site_url() into the jquery-ajax's url field.
Code: <$cript>
//contains the base URL of your installation
//e.g : http://yoursite.com/ci/
var base_url = '<?php echo base_url();?>';
//contains the base_url() and your front script(index.php)
//e.g : http://yoursite.com/ci/index.php
var site_url = '<?php echo site_url();?>';
$(document).ready(function(){
$("#submit").click(function(){
var username=$("#usr").val();
var password=$("#pass").val();
$.ajax({
type: "POST",
url: site_url+"/site/logval",
data: ({usr: username, pass: password}),
success: function(response){
if(response=='TRUE')
{
Redirect('site/home');
}
else
{
Redirect('site/login');
}
}
});
});
});
</$cript>
jquery ajax login in codeigniter..... - El Forum - 03-28-2012
[eluser]CodeIgniteMe[/eluser]
Next, Append the site_url variable to your redirect so that it redirects correctly
jquery ajax login in codeigniter..... - El Forum - 03-28-2012
[eluser]sivams20[/eluser]
I have changed the code as you said.But still its not working and shows no error...
jquery ajax login in codeigniter..... - El Forum - 03-28-2012
[eluser]CodeIgniteMe[/eluser]
Do you have Firebug installed? It's a great tool if you plan developing with ajax. It's an add-on for firefox. Install Firebug, then monitor the console tab. You'll see the output there.
jquery ajax login in codeigniter..... - El Forum - 03-28-2012
[eluser]CodeIgniteMe[/eluser]
Can you paste the new codes here so we can verify the effectiveness of your new codes?
jquery ajax login in codeigniter..... - El Forum - 03-28-2012
[eluser]CodeIgniteMe[/eluser]
Oh, now I see why there is no error displayed for your page. It's because you've used Redirect. Redirecting your page unsets the $_POST values to an empty array along with all validations rules and errors. I suggest you use jQuery more extensively, return data via JSON, XML or even a simple Text data to inform javascript of your backend status and messages, then append the message to your HTML output.
Can you please send us a code snippet of your view so we could continue with this?
jquery ajax login in codeigniter..... - El Forum - 03-28-2012
[eluser]sivams20[/eluser]
Here is my code for view.....
Code: <html>
<head>
<title>My Registration Home.....</title>
<link href="<?php echo base_url().'css/style.css' ; ?>" rel="stylesheet" type="text/css" />
<link href="<?php echo base_url().'css/form.css' ?>" rel="stylesheet" type="text/css"/>
$(document).ready(function()
{
$(".sign_in").click(function()
{
$("#sign_box").toggle('slow');
return false;
});
});
$(document).ready(function(){
$("#submit").click(function(){
var base_url = '<?php echo base_url();?>';
var site_url = '<?php echo site_url();?>';
var username=$("#usr").val();
var password=$("#pass").val();
$.ajax({
type: "POST",
url: "/site/logval",
data: ({usr: username, pass: password}),
success: function(response){
if(response== TRUE)
{
var new_url=site_url+"/site/home";
Redirect(new_url);
}
else
{
var new_url=site_url+"/site/login";
Redirect(new_url);
}
}
});
});
});
<!-- <link href="<?php echo base_url().'css/style.css' ; ?>" rel="stylesheet" type="text/css"/> -->
</head>
<body>
<menu id="navoption">
<a href="[removed] void(0);" class="sign_in">LOGIN</a>
<div id="sign_box">
<?php echo form_open('');?>
<p>
<label>UserName
<input type="text" name="usr" id="usr"/></label>
<?php echo form_error('usr');?>
</p>
<p>
<label>Password
<input type='password' name="pass" id="pass"/></label>
<?php echo form_error('pass'); ?>
</p>
<input type="button" id="submit" name="submit" value=" Sing In "/>
</form>
</div>
</menu>
</body>
</html>
jquery ajax login in codeigniter..... - El Forum - 03-28-2012
[eluser]CodeIgniteMe[/eluser]
Error messages will not be displayed yet if your AJAX code is as simple as this.
First, remove the line that returns false in your controller, so the entire HTML will be outputted from the server-side and is received by AJAX. Second, do not return true from your controller because AJAX/Javascript cannot read boolean from other language (such as PHP). Echo it instead, then exit to terminate the script. Your controller should look more like this
Code: if($this->user_model->check_login($data)== TRUE)
{
echo 'success';
exit();
//$this->load->view('home');
}
else
{
$this->load->view('failure');
//return FALSE;
}
Third, since the entire HTML is returned to AJAX if the login is incorrect, do this condition
Code: success: function(response){
if(response=='success')
{
alert('success');
}
else
{
$('form').prepend(response);
}
try it so you'll know if it will work
|