Javascript 从PHP文件发回消息
我正在创建一个注册表单,并将其设置为一旦用户单击submit按钮,它就会将所有表单信息发送到一个PHP文件,然后该文件会验证所有内容 我想做的是,如果多个输入有错误,我如何从PHP文件发送一条消息并将其显示在适当的输入旁边 以下是我目前掌握的情况: JavaScript:Javascript 从PHP文件发回消息,javascript,php,ajax,jquery,Javascript,Php,Ajax,Jquery,我正在创建一个注册表单,并将其设置为一旦用户单击submit按钮,它就会将所有表单信息发送到一个PHP文件,然后该文件会验证所有内容 我想做的是,如果多个输入有错误,我如何从PHP文件发送一条消息并将其显示在适当的输入旁边 以下是我目前掌握的情况: JavaScript: $(document).ready(function() { $(document).on("submit", "form", function(event) { event.preventDefault();
$(document).ready(function() {
$(document).on("submit", "form", function(event) {
event.preventDefault();
$.ajax({
url: 'assets/php/verify.php',
type: "POST",
data: $(this).serialize(),
success: function(data) {
alert(data);
}
});
});
});
PHP:
如何解码$errors数组,然后将值消息放在每个输入旁边(例如):
用户名不可用。
密码无效。
据我所知,php无法立即将错误消息放回网页。您可能想考虑使用Ajax来实现这一点。 < P>从我所知道的,PHP不能立即将错误消息放回网页。你可能想考虑使用Ajax来做这件事。 < P>你可以这样做:
// js
$.ajax({
url: 'assets/php/verify.php',
type: "POST",
data: $(this).serialize(),
}).then(function(data) {
if (data['errors']) {
$.each(data['errors'], function(key, val) {
$('#error-' + key).text(val);
});
}
});
// html
<span id="error-username"></span>
你可以这样做:
// js
$.ajax({
url: 'assets/php/verify.php',
type: "POST",
data: $(this).serialize(),
}).then(function(data) {
if (data['errors']) {
$.each(data['errors'], function(key, val) {
$('#error-' + key).text(val);
});
}
});
// html
<span id="error-username"></span>
照Joshua Smock说的做,别忘了给ajax添加属性
$.ajax({
url: 'assets/php/verify.php',
type: "POST",
dataType: "json", // look here
data: $(this).serialize(),
}). // continue
照Joshua Smock说的做,别忘了给ajax添加属性
$.ajax({
url: 'assets/php/verify.php',
type: "POST",
dataType: "json", // look here
data: $(this).serialize(),
}). // continue
通过使用数据类型:“json”
告诉jQuery您希望得到一个对象作为响应,jQuery然后将结果解析为一个对象,您可以作为数据访问该对象。property\u key
:
jQuery:
$.ajax({
type: "POST",
url: 'assets/php/verify.php',
data: $(this).serialize(),
success: function(data) {
if(data.status=="success"){
$("#message").html('Success, do something');
}else{
$("#message_user").html(data.username);
$("#message_pass").html(data.password);
$("#message_email").html(data.email);
}
},
dataType: "json"
});
然后将div改为span;p
<input name="username" type="text"><span id="message_user"></span>
<input name="password" type="text"><span id="message_pass"></span>
<input name="email" type="text"><span id="message_email"></span>
<span id="message"></span>
然后,您的PHP部分需要更多的验证来检查其他可能的错误。然后不要忘记json头
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$errors = array();
//username
if(!empty($_POST["username"])){
$username = $_POST["username"];
//do locic to check username ect
if(username already found in db){
$errors["username"] = "Username not available.";
}
}else{
$errors["username"] = "Username required.";
}
//email, should also check its a real email
if(!empty($_POST["email"])){
$email = $_POST["email"];
}else{
$errors["email"] = "Email required.";
}
//password
if(!empty($_POST["password"])){
$password = $_POST["password"];
}else{
$errors["password"] = "Password required.";
}
//do if $errors empty
if(empty($errors)){
//do somthing all was good
$responce = array('status'=>'success');
}else{
//error
$responce = $errors;
}
//send responce
header('Content-Type:application/json;');
echo json_encode($responce);
}
?>
通过使用数据类型:“json”
告诉jQuery您希望得到一个对象作为响应,jQuery然后将结果解析为一个对象,您可以作为数据访问。属性键
:
jQuery:
$.ajax({
type: "POST",
url: 'assets/php/verify.php',
data: $(this).serialize(),
success: function(data) {
if(data.status=="success"){
$("#message").html('Success, do something');
}else{
$("#message_user").html(data.username);
$("#message_pass").html(data.password);
$("#message_email").html(data.email);
}
},
dataType: "json"
});
然后将div改为span;p
<input name="username" type="text"><span id="message_user"></span>
<input name="password" type="text"><span id="message_pass"></span>
<input name="email" type="text"><span id="message_email"></span>
<span id="message"></span>
然后,您的PHP部分需要更多的验证来检查其他可能的错误。然后不要忘记json头
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$errors = array();
//username
if(!empty($_POST["username"])){
$username = $_POST["username"];
//do locic to check username ect
if(username already found in db){
$errors["username"] = "Username not available.";
}
}else{
$errors["username"] = "Username required.";
}
//email, should also check its a real email
if(!empty($_POST["email"])){
$email = $_POST["email"];
}else{
$errors["email"] = "Email required.";
}
//password
if(!empty($_POST["password"])){
$password = $_POST["password"];
}else{
$errors["password"] = "Password required.";
}
//do if $errors empty
if(empty($errors)){
//do somthing all was good
$responce = array('status'=>'success');
}else{
//error
$responce = $errors;
}
//send responce
header('Content-Type:application/json;');
echo json_encode($responce);
}
?>
我添加了dataType:'json'
,但是当我提交表单时,它不会回显任何内容。我添加了dataType:'json'
,但是当我提交表单时,它不会回显任何内容。