AJAX第一次在codeigniter中加载时无法正常工作

时间:2017-08-13 12:53:34

标签: php jquery ajax codeigniter

网页上有一个用户注册表格,其中包含四个字段:用户名,密码,确认密码&电子邮件。 使用AJAX实现用户名和电子邮件字段,以选择唯一的用户名和用户名。新用户的电子邮件,现在问题是当网页第一次在浏览器中加载AJAX for username&电子邮件无效,一旦您提交表单,整个系统将开始正常工作。我无法找到问题并且没有任何想法来解决这个问题。

有三个文件

  1. register.php 查看
  2. check_existing.php 控制器
  3. reg_model.php 型号
  4. register.php

    <script>
      $(document).ready(function(){
        $(".usernametxt").on('keyup keypress blur change', function(){
            $.ajax({
            type: "POST",
            url: "<?php echo base_url().'existing_username'; ?>",
            data:'user_name='+$(this).val(),
            success: function(data){
                $("#ex_username").html(data);
            }
            });
        });
      });
    </script>
    
    <script>
      $(document).ready(function(){
        $(".regemail").on('keyup keypress blur change', function(){
            $.ajax({
            type: "POST",
            url: "<?php echo base_url().'existing_email'; ?>",
            data:'reg_email='+$(this).val(),
            success: function(data){
                $("#reg_email_err").html(data);
            }
            });
        });
      });
    </script>
    
    <div class="form-header">
      <h1>Register Account</h1>
    </div>
    <div class="form-content">
      <form method="post" action="<?php echo base_url().'Welcome/register'; ?>">
        <div class="form-group">
          <label for="username">Username <div style="letter-spacing: 0px;text-transform:lowercase; color:rgba(255, 234, 0, 0.86); display:inline-block;" id="ex_username"></div></label>
          <input type="text" class="usernametxt" id="username" name="username" required="required" autocomplete="off" title="Enter your username"/>
        </div>
        <div class="form-group">
          <label for="password">Password <div style="letter-spacing: 0px;text-transform:lowercase; color:rgba(255, 234, 0, 0.86); display:inline-block;" id="passerr"></div></label>
          <input type="password" id="passwordtxt" name="password" required="required" title="Enter your password"/>
        </div>
        <div class="form-group">
          <label for="cpassword">Confirm Password <div style="letter-spacing: 0px;text-transform:lowercase; color:rgba(255, 234, 0, 0.86); display:inline-block;" id="cpasserr"></div></label>
          <input type="password" id="cpasswordtxt" name="cpassword" required="required" title="Confirm password"/>
        </div>
        <div class="form-group">
          <label for="email">Email Address <div style="letter-spacing: 0px;text-transform:lowercase; color:rgba(255, 234, 0, 0.86); display:inline-block;" id="reg_email_err"></div></label>
          <input type="email" id="email" class="regemail" name="email" required="required" autocomplete="off" title="Enter your email"/>
        </div>
        <div class="form-group">
          <button type="submit">Register</button>
            <? if($this->session->flashdata('showreg')){ 
            $path=base_url()."assets/images/sign-warning-icon.png";
            ?>
            <div style="letter-spacing: 0px;margin-top:10px;text-transform:lowercase; color:rgba(255, 234, 0, 0.86); display:inline-block;">
                <img height='17px' style='margin-bottom:-3px;' width='17px' src='<?=$path; ?>'>
                <?= $this->session->flashdata('showreg') ?>
            </div>
            <? } ?>
        </div>
      </form>
    </div>
    

    check_existing.php

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Existing_username extends CI_Controller {
    
    public function index()
    {
        $user_name = $_POST['user_name'];
        $regex='/^[a-z0-9_-]{4,15}$/';
        $regex2='/^[a-z0-9_-]{1,15}$/';
    
        if(strlen($user_name)==0)
        {
            echo "";
        }
        else if(strlen($user_name)>15)
        {
            $path=base_url()."assets/images/sign-warning-icon.png";
            echo "<img height='17px' style='margin-bottom:-3px;' width='17px' src='$path'> User Name Too Long.";
        }
        else if(strlen($user_name)<4)
        {
            if(!preg_match($regex2, $user_name) && strlen($user_name)<4)
            {
                $path=base_url()."assets/images/sign-warning-icon.png";
                echo "<img height='17px' style='margin-bottom:-3px;' width='17px' src='$path'> Only _ (Underscore) allowed.";
            }
            else
            {
                $path=base_url()."assets/images/sign-warning-icon.png";
                echo "<img height='17px' style='margin-bottom:-3px;' width='17px' src='$path'> User Name Too Short.";
            }
        }
        else if(!preg_match($regex2, $user_name) && strlen($user_name)>=4)
        {
            $path=base_url()."assets/images/sign-warning-icon.png";
            echo "<img height='17px' style='margin-bottom:-3px;' width='17px' src='$path'> Only _ (Underscore) allowed.";
        }
        else if(preg_match($regex, $user_name))
        {
            $this->load->model('Existing_model');
            if($this->Existing_model->exists_username($user_name))
            {
                $path=base_url()."assets/images/sign-warning-icon.png";
                echo "<img height='17px' style='margin-bottom:-3px;' width='17px' src='$path'> Username Is Not Available";
            }
            else
            {
                $path=base_url()."assets/images/sign-check-icon.png";
                echo "<img height='17px' style='margin-bottom:-3px;' width='17px' src='$path'>";
            }
        }
        else
        {
            $path=base_url()."assets/images/sign-check-icon.png";
            echo "<img height='17px' style='margin-bottom:-3px;' width='17px' src='$path'> Invalid username.";
        }
    }
    }
    

    reg_model.php

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Existing_model extends CI_Model
    {
    public function exists_username($un)
    {
        $this->load->database();
        $this->db->where('user_name', $un);
    
        $query = $this->db->get('users');
    
        if($query->num_rows >= 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public function exists_email($em)
    {
        $this->load->database();
        $this->db->where('email', $em);
    
        $query = $this->db->get('users');
    
        if($query->num_rows >= 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    }
    ?>
    

    此索引.JS文件也包含

    &#13;
    &#13;
    $(document).ready(function() {
      var panelOne = $('.form-panel.two').height(),
        panelTwo = $('.form-panel.two')[0].scrollHeight;
    
      $('.form-panel.two').not('.form-panel.two.active').on('click', function(e) {
        
    
        $('.form-toggle').addClass('visible');
        $('.form-panel.one').addClass('hidden');
        $('.form-panel.two').addClass('active');
        $('.form').animate({
          'height': panelTwo
        }, 200);
      });
      $('.register_link').not('.form-panel.two.active').on('click', function(e) {
        
    
        $('.form-toggle').addClass('visible');
        $('.form-panel.one').addClass('hidden');
        $('.form-panel.two').addClass('active');
        $('.form').animate({
          'height': panelTwo
        }, 200);
      });
    
      $('.form-toggle').on('click', function(e) {
        
        $(this).removeClass('visible');
        $('.form-panel.one').removeClass('hidden');
        $('.form-panel.two').removeClass('active');
        $('.form').animate({
          'height': panelOne
        }, 200);
      });
    
      $('.forg-pass-link').on('click', function(e) {
        $('.form-panel.three').show();
        $('.form-panel.one').hide();
      });
    
      $('.forg-form-toggle').on('click', function(e) {
        $('.form-panel.three').hide();
        $('.form-panel.one').show();
      });
    });
    &#13;
    &#13;
    &#13;

1 个答案:

答案 0 :(得分:0)

我发现了这个问题,想知道为什么会发生这种情况。使用codeigniter。

我已经取代了

网址:&#34; &LT; ?php echo base_url()。&#39; existing_email&#39 ;; ?&GT;&#34;,

使用

网址:&#34; existing_email&#34;;

它开始运作良好。但我不明白为什么会这样呢