简体   繁体   中英

ajax is not working in codeigniter, doesn't post or start the method in the controller

I tried to search for my problem, but didn't find anything, also can't debug this, I tried to put breakpoints in do_login method and $("#frm_login").submit(function(event) it doesn't even get to it here it is the code for the signin_view Ajax script

<script type="text/javascript">
$(document).ready(function() {
$("#frm_login").submit(function(event)
{
event.preventDefault();
$.ajax({
url: '<?php echo site_url('signin/do_login'); ?>',
type: 'POST',
data:{ username : $('#username').val(),password : $('#password').val()},
success:function(data) {
    if (data ==='logged_in')
    {
        alert("working");
    }
    else if(data === 'no')
    {
                    alert("problem");

    }
}
});
           });
                          });

and here it is the form

  <form id='frm_login' name='frm_login' action="#" >

                    <input type="text" id='username' name="username" size="15" placeholder="اسم المستخدم">                              

<input type="password" id="password" name="password" size="15" placeholder="كلمة المرور" >                              
                            <div id="message"> </div>   
                            <input type='submit' value="تسجيل الدخول">
    </form>

and this is signin controller

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class signin extends CI_Controller
{



function __construct(){
    parent::__construct();
}
public function index(){

$this->load->view('signin_view');
}

function do_login()
{
{$this->load->model("M_accces");

$POSTINGDATA = $this->input->post('username');
if(isset($POSTINGDATA))
{
$user = $this->m_access->check_user(
        $this->input->post('username'),
        md5($this->input->post('password'))
        );
if($user == '1') {
echo 'logged_in';
} else {
echo 'no';
    }
}

and here is the M_accces model

class M_access extends CI_Model {
public function check_user($username,$password) {
$this->query = $this->db->select('COUNT(*)')
    ->from('users')
    ->where(array('username'=>$username,'password'=>$password))
    ->limit(1)->get();
return $this->query->row_array();
}
}

the autoloaded

$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url','file','form','security');

Can any one help why it is not working ?

The page is just refreshing. Thanks

you forgot to load the model in the controller

$this->load->model("M_accces");

you can put it just after do_login() function/ method, or in the constructor

salam

Remove the document ready function and drag your js function and place it after closing the form and it will work. Note: Press f12 in your browser and select network and see if the request is successfully sent or not.

If request is successfully sent and the program is still not working , click on the request then preview you will see the error shown.

Your Ajax Request has wrong selector used

$.ajax({
url: '<? echo site_url('signin/do_login'); ?>',
type: 'POST',
async : false,
// Note # selector.
data:{ username : $('#username').val(),password : $('#password').val()},
success:function(data) {
    if (data ==='logged_in')
    {
        alert("working");
    }
    else if(data === 'no')
    {
                    alert("problem");

    }
}
});

In your controller method check for $POSTINGDATA for empty value.

function do_login()
{
$POSTINGDATA = $this->input->post('username');
// check for empty. 
if(isset($POSTINGDATA) && !empty($POSTINGDATA))
{
$user = $this->m_access->check_user(
        $this->input->post('username'),
        md5($this->input->post('password'))
        );
if($user == '1') {
echo 'logged_in';
} else {
echo 'no';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM