dtvam48220 2015-01-01 17:48
浏览 57

使用codeigniter中的ajax检查已存在的电子邮件或用户名

I have create a form which contain just only one input tag which name is first_name .

Now I want to show the first_name is either exist in database or not.

I have create a database name db_practice ,table which name is tbl_user and there are many field. first_name is one of the field name.

Here is my all code. Please help me to find out my problem.

This is view code:

<html>
    <head>
        <meta charset="UTF-8">
        <title>Check username availability using jQuery, AJAX</title>

        <script type="text/javascript">
    $(document).ready(function() {
        $("#username").live("blur", function(e) {
            $('#msg').hide();
            if ($('#username').val() == null || $('#username').val() == "") {
                $('#msg').show();
                $("#msg").html("Username is required field.").css("color", "red");
            } else {
                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url();?>"+"/welcome/get_user",
                    data: $('#signupform').serialize(),
                    dataType: "html",
                    cache: false,
                    success: function(msg) {
                        $('#msg').show();
                        $("#msg").html(msg);
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        $('#msg').show();
                        $("#msg").html(textStatus + " " + errorThrown);
                    }
                });
            }
        });
    });
</script>
    </head>
    <body>
        <div style="margin: 10px 0 0 10px;width: 600px">
            <h3>Codeigniter username availability check</h3>
            <form id="signupform" style="padding: 10px;">
                <fieldset>
                    <legend>Check username</legend>
                    <div>
                        <label>Username</label><br/>
                        <input type="text" name="first_name" id="username"/>
                        <div id="msg"></div>
                    </div>
                </fieldset>
            </form>
        </div>
        <script src="<?php echo assets_url(); ?>js/jquery-1.9.1.min.js"></script>
        <script src="<?php echo assets_url(); ?>js/jquery-migrate-1.2.1.js"></script>
        <script src="<?php echo assets_url(); ?>js/jquery-ui-1.10.3-custom.min.js">
        </script>
    </body>

</html>
<!-- below jquery things triggered on onblur event and checks the username availability in the database -->

This is controller code:

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

class Welcome extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('my_model');
    }
    public function index()
    {
        $this->load->view('welcome_message');
    }
        public function get_user()
    {             
          if (isset($_POST['first_name']))
              {
                $username = $_POST['first_name'];
                $results = $this->my_model->get_user($username);
                if ($results === TRUE)
                    {
                        echo '<span style="color:red;">Username unavailable</span>';
                    }
                    else
                        {
                            echo '<span style="color:green;">Username available</span>';
                        }
             } 
             else {
                    echo '<span style="color:red;">Username is required field.</span>';
                  }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

This is model code:

<?php

/*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */

/**  * Description of my_model  *  * @author Masud  */ class My_Model extends CI_Model {
    //put your code here
    public function get_user($username)
    {
        $this->db->select('*');
        $this->db->from('tbl_user');
        $this->db->where('first_name',$username);
        $query = $this->db->get();
        $result = $query->row();
        if ($result)
        {
         return TRUE;
        }
        else 
         {
            return FALSE;
        }

        } }

?>
  • 写回答

3条回答 默认 最新

  • duanhai1455 2015-01-01 20:46
    关注

    Fix:

    1. In view: url: "<?php echo base_url(); ?>"+"/welcome/get_user",
    2. In controller: $results = $this->my_model->get_user($username); // not My_model, it's my_model
    3. In model:
      1. class My_model extends CI_Model { // My_model not My_Model
      2. $result = $query->row(); // $query, not $query_result
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度