AJAX 寄存器没有更新 div
AJAX register doesn't update div
所以我正在制作注册表单,并且我有 ajax 脚本来验证和提交数据。
这是代码:
$('document').ready(function() {
/* validation */
$("#register-form").validate({
rules: {
username: {
required: true,
rangelength: [4, 12]
},
email: {
required: true,
email: true
},
password: {
required: true,
rangelength: [4, 20]
},
password_repeat: {
required: true,
equalTo: "#password"
},
},
messages: {
username: {
required: "*Моля въведете потребителско име",
rangelength: "*Между 4 и 12 знака"
},
email: {
required: "*Моля въведете имейл"
},
password: {
required: "*Моля въведете парола",
rangelength: "*Между 8 и 20 знака"
},
password_repeat: {
required: "*Моля въведете паролата отново",
equalTo: "*Паролите не съвпадат"
},
},
submitHandler: submitForm
});
/* Handling login functionality */
function submitForm() {
var data = $("#register-form").serialize();
$.ajax({
type: 'POST',
url: 'auth_logic/register-process.php',
data: data,
beforeSend: function() {
$("#error").fadeOut();
$("#register").html('<img src="images/loading.gif" width="31px" height="31px" /> Моля изчакайте ...');
},
success: function(response) {
if (response == "ok") {
$("#register").html('<img src="images/loading.gif" width="31px" height="31px" /> Моля изчакайте ...');
$("#error").html('<div class="alert alert-danger"> ' + response + '</div>');
//setTimeout(' window.location.href = "/auth/login"; ',100);
} else if (response == "Потребителското име е заето!") {
$("#error").fadeIn(100, function() {
$("#error").html('<div class="alert alert-danger"> ' + response + '</div>');
$("#register").html(' Регистрация');
});
} else if (response == "Този имейл е регистриран вече!") {
$("#error").fadeIn(100, function() {
$("#error").html('<div class="alert alert-danger"> ' + response + '</div>');
$("#register").html(' Регистрация');
});
}
}
});
return false;
}
});
PHP 部分工作得很好,但是当我想更新“#error”时 div 我之前的表单不起作用,但它更新了提交按钮。
表格如下:
<div class="container py-5">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-6 mx-auto">
<div class="card border-secondary">
<div class="card-header">
<center><h3 class="mb-0 my-2">Нов потребител</h3></center>
</div>
<div class="card-body">
<div id="error"></div>
<form class="form-group" method="POST" id="register-form">
<div class="form-group">
<label for="username">Потребител:</label>
<input type="text" class="form-control" name="username" id="username" placeholder="Потребителско име" >
</div>
<div class="form-group">
<label for="email">Имейл:</label>
<input type="email" class="form-control" name="email" id="email" placeholder="Имейл адрес" >
</div>
<div class="form-group">
<label for="password">Парола:</label>
<input type="password" class="form-control" name="password" id="password" placeholder="Парола">
</div>
<div class="form-group">
<label for="password_again">Парола отново:</label>
<input type="password" class="form-control" name="password_repeat" id="password_repeat" placeholder="Повторете паролата" >
</div>
<div class="form-group">
<button type="submit" name="register" id="register" class="btn btn-block btn-success btn-lg float-right">Регистрация</button>
</div>
</form>
</div>
<div class="card-footer">
<a href="login">Вход</a>
</div>
</div>
</div>
</div>
<!--/row-->
</div>
<!--/col-->
</div>
<!--/row-->
我清除缓存刷新 400 次,重命名文件。我正在使用 XAMPP
因为 beforeSend:
你用 .fadeOut();
隐藏了 #error
在 success:
上,您替换了 #error 中的内容,但它仍然不可见。
您需要添加类似 $('#error').show();
或包裹在里面的内容:
$("#error").fadeIn(100, function() {
}
所以我正在制作注册表单,并且我有 ajax 脚本来验证和提交数据。 这是代码:
$('document').ready(function() {
/* validation */
$("#register-form").validate({
rules: {
username: {
required: true,
rangelength: [4, 12]
},
email: {
required: true,
email: true
},
password: {
required: true,
rangelength: [4, 20]
},
password_repeat: {
required: true,
equalTo: "#password"
},
},
messages: {
username: {
required: "*Моля въведете потребителско име",
rangelength: "*Между 4 и 12 знака"
},
email: {
required: "*Моля въведете имейл"
},
password: {
required: "*Моля въведете парола",
rangelength: "*Между 8 и 20 знака"
},
password_repeat: {
required: "*Моля въведете паролата отново",
equalTo: "*Паролите не съвпадат"
},
},
submitHandler: submitForm
});
/* Handling login functionality */
function submitForm() {
var data = $("#register-form").serialize();
$.ajax({
type: 'POST',
url: 'auth_logic/register-process.php',
data: data,
beforeSend: function() {
$("#error").fadeOut();
$("#register").html('<img src="images/loading.gif" width="31px" height="31px" /> Моля изчакайте ...');
},
success: function(response) {
if (response == "ok") {
$("#register").html('<img src="images/loading.gif" width="31px" height="31px" /> Моля изчакайте ...');
$("#error").html('<div class="alert alert-danger"> ' + response + '</div>');
//setTimeout(' window.location.href = "/auth/login"; ',100);
} else if (response == "Потребителското име е заето!") {
$("#error").fadeIn(100, function() {
$("#error").html('<div class="alert alert-danger"> ' + response + '</div>');
$("#register").html(' Регистрация');
});
} else if (response == "Този имейл е регистриран вече!") {
$("#error").fadeIn(100, function() {
$("#error").html('<div class="alert alert-danger"> ' + response + '</div>');
$("#register").html(' Регистрация');
});
}
}
});
return false;
}
});
PHP 部分工作得很好,但是当我想更新“#error”时 div 我之前的表单不起作用,但它更新了提交按钮。
表格如下:
<div class="container py-5">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-6 mx-auto">
<div class="card border-secondary">
<div class="card-header">
<center><h3 class="mb-0 my-2">Нов потребител</h3></center>
</div>
<div class="card-body">
<div id="error"></div>
<form class="form-group" method="POST" id="register-form">
<div class="form-group">
<label for="username">Потребител:</label>
<input type="text" class="form-control" name="username" id="username" placeholder="Потребителско име" >
</div>
<div class="form-group">
<label for="email">Имейл:</label>
<input type="email" class="form-control" name="email" id="email" placeholder="Имейл адрес" >
</div>
<div class="form-group">
<label for="password">Парола:</label>
<input type="password" class="form-control" name="password" id="password" placeholder="Парола">
</div>
<div class="form-group">
<label for="password_again">Парола отново:</label>
<input type="password" class="form-control" name="password_repeat" id="password_repeat" placeholder="Повторете паролата" >
</div>
<div class="form-group">
<button type="submit" name="register" id="register" class="btn btn-block btn-success btn-lg float-right">Регистрация</button>
</div>
</form>
</div>
<div class="card-footer">
<a href="login">Вход</a>
</div>
</div>
</div>
</div>
<!--/row-->
</div>
<!--/col-->
</div>
<!--/row-->
我清除缓存刷新 400 次,重命名文件。我正在使用 XAMPP
因为 beforeSend:
你用 .fadeOut();
#error
在 success:
上,您替换了 #error 中的内容,但它仍然不可见。
您需要添加类似 $('#error').show();
或包裹在里面的内容:
$("#error").fadeIn(100, function() {
}