1

CodeIgniter Ajax 不适合我。

这是我到目前为止所尝试的。

v_login.php

         <script type="application/javascript">
    $(document).ready(function() {
      $('#submit').click(function() {
        var form_data = {
        username : $('#username').val(),
        password : $('#password').val(),
        ajax : '1'
      };
      $.ajax({
        url: "<?php echo site_url('c_login/ajax_check'); ?>",
        type: 'POST',
        async : false,
        data: form_data,
        success: function(msg) {
          $('#message').html(msg);
        }
      });
        return false;
      });
    });
  </script>

  <div class="container">
     <div class="jumbotron">

     <?php 
     $attributes = array('class' => 'form-signin');
     echo form_open('c_login', $attributes); ?>
        <h2 class="form-signin-heading">VMS Login System</h2>
          <input type="username" name="username" id="username" class="form-control" placeholder="Username" required autofocus>
          <input type="password" name="password" class="form-control" placeholder="Password" required>
          <input class="btn btn-primary" type="submit" id="submit" value="Login">
          <input class="btn btn-primary" type="reset" value="Cancel">
       <?php echo form_close(); ?>
        <div id="message">
        </div>

     </div>
  </div> 

C_login.php

  <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class C_login extends CI_Controller {

        function __construct() {

            parent::__construct();

            $this->load->library('form_validation');
            $this->load->helper('url');

        }
        function index() {
            $this->load->view('include/header');
            $this->load->view('v_login');
            $this->load->view('include/footer');
        }

        function ajax_check() {
            if($this->input->post('ajax') == '1') {
                $this->form_validation->set_rules('username', 'username', 'trim|required|xss_clean');
                $this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean');
                $this->form_validation->set_message('required', 'Please fill in the fields');
            if($this->form_validation->run() == FALSE) {
                echo validation_errors();
            } else {
                $this->load->model('m_access');
                $user = $this->m_access->check_user($this->input->post('username'),$this->input->post('password'));
            if($user == '1') {
                echo 'login successful';
            } else {
                echo 'unknown user';
            }
            }
            }
        }
    }
    /* End of file c_login.php */
    /* Location: ./application/controllers/c_login.php */

m_access.php

   <?
    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();
        }
    }

不知道怎么回事,已经设置好config.php和路由了。但它根本不起作用。有任何想法吗?非常感谢您的帮助。谢谢。

4

2 回答 2

1

经过长时间chatOP解决方案是这样的

你的模型

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

在您的控制器中,使用

$this->load->model('m_access');
$user = $this->m_access->check_user($this->input->post('username'), $this->input->post('password'));
if($user) {
    // right user
}
else {
    // wrong user
}

不需要发送ajax:1,因为jQuery向服务器发送请求标头,像这样

X-Requested-With': 'XMLHttpRequest'

并且CodeIgniter您可以使用检查 ajax 请求

if($this->input->is_ajax_request()){
    // it's an ajax request
}

另外,您要求gif加载程序来 shw,因此可以将其用作

$('#submit').click(function() {
    var form_data = { ...};
    // show the gif loader
    $.ajax({
        ...
        data: form_data,
        success: function(msg) {
          // hide the gif loeader
          $('#message').html(msg);
        }
    });
});

更新:要插入图像,您可以查看此小提琴示例,例如

$('#submit').click(function(e){
    e.preventDefault();
    var loader = $('<img/>', {
        'src':'url_of_the_image',
        'id':'ajax_loader'
    });
    loader.insertAfter($(this));
});

并删除success回调中的图像,您可以使用

$('#ajax_loader').remove();
于 2013-11-08T18:36:17.977 回答
0

由于调用路径导致的错误 404,因为文件名使用“C”而您使用“c”调用它,而不是在我看来,您需要在尝试 ajax 调用之前更深入地了解 codeigniter表单验证帮助程序

一旦你完美地做到了服务器端&如果你仍然想使用 ajax 进行验证,那么沿着原始服务器端验证功能将另一个功能作为 web 服务仅用于此目的,它会是这样的

     function ajax_check(){
     $username_check=true;
     //your code goes here to check if username unique or not and result assigned to $username_check 
     if(!$username_check){
       echo json_encode('fail');
       return;
       }
     echo json_encode('success');
     }
于 2013-11-08T18:46:19.917 回答