[英]AJAX not working for the first time page loads in codeigniter
A webpage has a form for user registration which contains four fields username, password, confirm password & email. 网页具有用于用户注册的表格,该表格包含四个字段:用户名,密码,确认密码和电子邮件。 An Username and Email fields are implemented with AJAX to choose unique username & email for new user, now the problem is when webpage loads first time in the browser the AJAX for username & email not works, once you submitted the form whole system will starts working properly. 使用AJAX实现了“用户名”和“电子邮件”字段,以便为新用户选择唯一的用户名和电子邮件,现在的问题是,当网页首次在浏览器中加载时,用于用户名和电子邮件的AJAX无效,一旦您提交了表单,整个系统将开始工作正常。 I can't find the problem and don't have any idea to solve this problem. 我找不到问题,也没有解决这个问题的想法。
There are three files 一共有三个文件
register.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 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 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;
}
}
}
?>
THIS INDEX.JS FILE IS ALSO INCLUDED 此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(); }); });
i found the problem and want to know why this is happen with codeigniter. 我发现了问题,想知道为什么Codeigniter会发生这种情况。
I have replaced 我已更换
url: " < ?php echo base_url().'existing_email'; ?>", url:“ <?php echo base_url()。'existing_email';?>”,
WITH WITH
url: "existing_email"; 网址:“ existing_email”;
and it is starts working well and good. 它开始运作良好。 But i cant understand why this is happen 但我不明白为什么会这样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.