【问题标题】:AJAX not working for the first time page loads in codeigniterAJAX 在 codeigniter 中首次加载页面时不起作用
【发布时间】:2017-08-14 13:34:14
【问题描述】:

一个网页有一个用户注册表格,其中包含用户名、密码、确认密码和电子邮件四个字段。 用户名和电子邮件字段是用 AJAX 实现的,可以为新用户选择唯一的用户名和电子邮件,现在的问题是当网页第一次在浏览器中加载时,用户名和电子邮件的 AJAX 不起作用,一旦你提交了表单,整个系统就会开始工作适当地。我找不到问题,也没有任何想法来解决这个问题。

一共有三个文件

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

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

此 INDEX.JS 文件也包含在内

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

【问题讨论】:

  • 您不应该只使用其中一种吗? "keyup keypress 模糊变化"
  • 它不起作用:(
  • 您检查了$.ajax() 中的网址,它是否从php 正确发送?在这一点上url: "&lt;?php echo base_url().'existing_username'; ?&gt;"。很多时候我对此有疑问,我必须做什么,我把它放在隐藏字段中,并在 ajax 调用中通过 ID 调用它。希望对您有所帮助:)

标签: php jquery ajax codeigniter


【解决方案1】:

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

我换了

url:" ",

网址:“existing_email”;

它开始运作良好。但我不明白为什么会发生这种情况

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2015-04-08
    • 1970-01-01
    相关资源
    最近更新 更多