我有一个注册页面,在这里我用jquery验证验证所有字段条目。如何使用jquery验证我的表单以检查mysql数据库中的副本?我试图使用远程方法,但当我键入已经在数据库中的电子邮件时,不会发生任何事情。
sign_up.php
$("#feedbackForm").validate({
rules: {
email: {
required: true,
email: true,
remote: "inc/check_email.php"
}
},
messages: {
email: {
email: "Insert valid adress",
required: "Please complete email field",
remote: "Email is already in use, please change it."
}
}
});/inc/check_email.php
<?php
include('config.php');
$email = $_POST['email'];
$query = mysql_query("SELECT * FROM table WHERE email = '" . $email . "'");
if (mysql_num_rows($query) > 0) {
return true;
} else{
return false;
}发布于 2016-01-25 12:50:50
插件期待的是一个JSON,谢谢大家。
if (mysql_num_rows($query) > 0) {
echo json_encode(FALSE);
} else{
echo json_encode(TRUE);
}发布于 2016-01-25 12:29:58
来自文档
服务器端响应必须是一个JSON字符串,对于有效元素必须是"true“,对于无效元素可以是"false”、未定义或null,使用默认的错误消息。如果服务器端响应是字符串,例如。“该名称已被使用,请尝试使用peter123”,此字符串将显示为自定义错误消息而不是默认值。
因此,您需要将if更改为:
$array = ['email' => 'true'];
if (mysql_num_rows($query) > 0) {
$array['email'] = 'Email is duplicated';
}
echo json_encode($array);此外,您应该将db引擎更改为pdo或mysqli。不推荐使用Mysql_*函数。
发布于 2016-01-25 12:33:07
看这个网址
http://jqueryvalidation.org/files/demo/ajaxSubmit-integration-demo.html
jQuery(function() {
$.mockjax({
url: "login.action",
response: function(settings) {
var user = settings.data.match(/user=(.+?)($|&)/)[1],
password = settings.data.match(/password=(.+?)($|&)/)[1];
if (password !== "foobar") {
this.responseText = "Your password is wrong (must be foobar).";
return;
}
this.responseText = "Hi " + user + ", welcome back.";
},
responseStatus: 200,
responseTime: 500
});
// show a simple loading indicator
var loader = jQuery('<div id="loader"><img src="images/loading.gif" alt="loading..."></div>')
.css({
position: "relative",
top: "1em",
left: "25em",
display: "inline"
})
.appendTo("body")
.hide();
jQuery().ajaxStart(function() {
loader.show();
}).ajaxStop(function() {
loader.hide();
}).ajaxError(function(a, b, e) {
throw e;
});
var v = jQuery("#form").validate({
submitHandler: function(form) {
jQuery(form).ajaxSubmit({
target: "#result"
});
}
});
jQuery("#reset").click(function() {
v.resetForm();
});
});https://stackoverflow.com/questions/34992514
复制相似问题