我已成功创建了一个表单,可以将用户提交并添加到mysql数据库中。使用'jQuery Validator'插件进行表单验证非常好,只有检查用户名是否已存在于数据库中时出现问题...
我刚刚花了大约8个小时阅读和尝试找出一种使用'jQuery Validator'插件定义新方法的方法。我似乎无法理解如何通过jQuery检查输入的用户名或电子邮件,并返回它是否已存在于数据库中。
我的代码:
如果有聪明人能够修改我的代码并告诉我应该如何做,那将是非常有帮助的 - 我感觉自己快要疯了!提前感谢,迫不及待地想看到解决方案 :-)
我刚刚花了大约8个小时阅读和尝试找出一种使用'jQuery Validator'插件定义新方法的方法。我似乎无法理解如何通过jQuery检查输入的用户名或电子邮件,并返回它是否已存在于数据库中。
我的代码:
<script src="../assets/js/main.js"></script>
<script src="../assets/js/ajax.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.min.js"></script>
<!-- FORM VALIDATION -->
<script type="text/javascript">
jQuery.validator.addMethod("checkExists",
function(value, element) {
//No idea what to call here
},
"Username already exists."
);
//<![CDATA[
$(window).load(function(){
$("form").validate({
rules: {
username: {minlength: 3, required: true, checkExists: true},
email: {email: true, required: true},
pass1: {minlength: 3, required: true},
pass2: {minlength: 3, required: true, equalTo: "#pass1"},
country: {required: true},
tandc: {required: true},
},
messages: {
username: {required: "You need to enter a Username."},
email: {required: "You need to enter an Email Address."},
pass1: {required: "You need to enter a Password."},
pass2: {required: "You need to enter your password again.", equalTo: "Your passwords don't match."},
country: {required: "You need to tell us where you live."},
tandc: {required: "You need to read and agree to the Terms and Conditions to use CGE."}
},
showErrors: function(errorMap, errorList) {
$.each(this.successList, function(index, value) {
return $(value).popover("hide");
});
return $.each(errorList, function(index, value) {
var _popover;
console.log(value.message);
_popover = $(value.element).popover({
trigger: "manual",
placement: "right",
content: value.message,
template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-inner\"><div class=\"popover-content\"><p></p></div></div></div>"
});
_popover.data("popover").options.content = value.message;
return $(value.element).popover("show");
});
}
});
});//]]>
</script>
如果有聪明人能够修改我的代码并告诉我应该如何做,那将是非常有帮助的 - 我感觉自己快要疯了!提前感谢,迫不及待地想看到解决方案 :-)
编辑 - 这是我的当前代码
似乎完全没有反应,但我感觉我离成功更近了:
当前代码:
signup.php
$(window).load(function(){
$("form").validate({
rules: {
username: {minlength: 3, required: true},
email: {email: true, required: true, remote: {url: "./validation/checkUnameEmail.php", type : "post"}},
pass1: {minlength: 3, required: true},
pass2: {minlength: 3, required: true, equalTo: "#pass1"},
country: {required: true},
tandc: {required: true}
},
checkUnameEmail.php
<?php
include_once(".../php_includes/db_conx.php");
$email = urldecode($_POST['email']);
$result = mysqli_query($db_conx, "SELECT * FROM users WHERE email = '$email' LIMIT 1;");
$num = mysqli_num_rows($result);
if($num == 0){
echo "true";
} else {
echo "E-Mail-Adresse schon registriert.";
}
mysqli_close($db_conx);
?>
*db_conx.php*
<?php
$db_conx = mysqli_connect("localhost", "root", "root", "membership");
//Evlauate the connection
if (mysqli_connect_errno()){
echo mysqli_connect_error();
exit();
}
?>