我正在为我的网站设置用户注册表单。我正在使用mysql数据库来存储我的用户,并且已经设置并运行了注册表单。我使用jQuery Validator对所有内容进行了验证,包括重复的用户名和电子邮件地址。如果有任何空白字段或未选中“同意条款”复选框,我已使用Javascript来禁用提交按钮。但如果有重复的用户名或电子邮件地址,我无法弄清楚如何禁用正在提交的表单。
我最近的尝试是试图读取jQuery验证器错误弹出窗口的内容,如果它会通过空,但当我输入一个全新的用户名时我当然得到一个错误,因为我正在检查的div没有存在:
else if (_("duplicate_content").value != ""){
无论如何这里是我的代码,对不起,如果它是一团糟! : - )
<?php
session_start();
// If user is logged in, header them away
if(isset($_SESSION["username"])){
header("location: message.php?msg=NO to that weenis");
exit();
}
?><?php
// Ajax calls this NAME CHECK code to execute
if(isset($_POST["usernamecheck"])){
include_once("../php_includes/db_conx.php");
$username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']);
$sql = "SELECT id FROM users WHERE username='$username' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$uname_check = mysqli_num_rows($query);
if (strlen($username) < 3 || strlen($username) > 16) {
echo '<strong style="color:#F00;">3 - 16 characters please</strong>';
exit();
}
if (is_numeric($username[0])) {
echo '<strong style="color:#F00;">Usernames must begin with a letter</strong>';
exit();
}
if ($uname_check < 1) {
exit();
} else {
exit();
}
}
?><?php
// Ajax calls this REGISTRATION code to execute
if(isset($_POST["u"])){
// CONNECT TO THE DATABASE
include_once("../php_includes/db_conx.php");
// GATHER THE POSTED DATA INTO LOCAL VARIABLES
$u = preg_replace('#[^a-z0-9]#i', '', $_POST['u']);
$e = mysqli_real_escape_string($db_conx, $_POST['e']);
$p = $_POST['p'];
$c = preg_replace('#[^a-z ]#i', '', $_POST['c']);
// GET USER IP ADDRESS
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// DUPLICATE DATA CHECKS FOR USERNAME AND EMAIL
$sql = "SELECT * FROM users WHERE username='$u' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$u_check = mysqli_num_rows($query);
// -------------------------------------------
$sql = "SELECT id FROM users WHERE email='$e' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$e_check = mysqli_num_rows($query);
// FORM DATA ERROR HANDLING
if($u == "" || $e == "" || $p == "" || $c == ""){
echo "The form submission is missing values.";
exit();
} else if ($u_check > 0){
echo "The username you entered is alreay taken";
exit();
} else if ($e_check > 0){
echo "That email address is already in use in the system";
exit();
} else if (strlen($u) < 3 || strlen($u) > 16) {
echo "Username must be between 3 and 16 characters";
exit();
} else if (is_numeric($u[0])) {
echo 'Username cannot begin with a number';
exit();
} else {
// END FORM DATA ERROR HANDLING
// Begin Insertion of data into the database
// Hash the password and apply your own mysterious unique salt
$cryptpass = crypt($p);
include_once ("../php_includes/randStrGen.php");
$p_hash = randStrGen(20)."$cryptpass".randStrGen(20);
// Add user info into the database table for the main site table
$sql = "INSERT INTO users (username, email, password, country, ip, signup, lastlogin, notescheck)
VALUES('$u','$e','$p_hash','$c','$ip',now(),now(),now())";
$query = mysqli_query($db_conx, $sql);
$uid = mysqli_insert_id($db_conx);
// Establish their row in the useroptions table
$sql = "INSERT INTO useroptions (id, username, background) VALUES ('$uid','$u','original')";
$query = mysqli_query($db_conx, $sql);
// Create directory(folder) to hold each user's files(pics, MP3s, etc.)
if (!file_exists("user/$u")) {
mkdir("user/$u", 0755);
}
// Email the user their activation link
$to = "$e";
$from = "auto_responder@yoursitename.com";
$subject = 'yoursitename Account Activation';
$message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>yoursitename Message</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://www.yoursitename.com"><img src="http://www.yoursitename.com/images/logo.png" width="36" height="30" alt="yoursitename" style="border:none; float:left;"></a>yoursitename Account Activation</div><div style="padding:24px; font-size:17px;">Hello '.$u.',<br /><br />Click the link below to activate your account when ready:<br /><br /><a href="http://www.yoursitename.com/activation.php?id='.$uid.'&u='.$u.'&e='.$e.'&p='.$p_hash.'">Click here to activate your account now</a><br /><br />Login after successful activation using your:<br />* E-mail Address: <b>'.$e.'</b></div></body></html>';
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
mail($to, $subject, $message, $headers);
echo "signup_success";
exit();
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="This is the description">
<meta name="author" content="David Colley">
<!-- Le styles -->
<link href="../assets/css/bootstrap.css" rel="stylesheet">
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
</style>
<link href="../assets/css/bootstrap-responsive.css" rel="stylesheet">
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="./assets/js/html5shiv.js"></script>
<![endif]-->
<!-- Fav and touch icons -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="../assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="../assets/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="../assets/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="../acybssets/ico/apple-touch-icon-57-precomposed.png">
<link rel="shortcut icon" href="../assets/ico/favicon.png">
<script src="../assets/js/main.js"></script>
<script src="../assets/js/ajax.js"></script>
<script>
function restrict(elem){
var tf = _(elem);
var rx = new RegExp;
if(elem == "email"){
rx = /[' "]/gi;
} else if(elem == "username"){
rx = /[^a-z0-9_]/gi;
}
tf.value = tf.value.replace(rx, "");
}
function emptyElement(x){
_(x).innerHTML = "";
}
function checkusername(){
var u = _("username").value;
if(u != ""){
var ajax = ajaxObj("POST", "signup.php");
ajax.onreadystatechange = function() {
}
ajax.send("usernamecheck="+u);
}
}
function signup(){
var u = _("username").value;
var e = _("email").value;
var p1 = _("pass1").value;
var p2 = _("pass2").value;
var c = _("country").value;
var status = _("status");
if(u == "" || e == "" || p1 == "" || p2 == "" || c == ""){
status.innerHTML = "";
} else if(p1 != p2){
status.innerHTML = "";
} else if (!document.getElementById('tandc').checked){
status.innerHTML = "";
} else if (_("duplicate_content").value != ""){
status.innerHTML = "";
}
else {
_("signupbtn").style.display = "none";
status.innerHTML = 'please wait ...';
var ajax = ajaxObj("POST", "signup.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
var ajxr = ajax.response.trim();
if(ajax.responseText.replace(/^\s+|\s+$/g, "") == "signup_success"){
status.innerHTML = ajxr;
_("signupbtn").style.display = "block";
} else {
window.scrollTo(0,0);
_("signupform").innerHTML = "<img style='margin: -80px 0 0 -135px' src='../assets/img/logo.png' /><h2 class='form-signin-heading'>Account created</h2><p>OK <b>"+u+"</b>!<br><br>Check your email inbox and junk mail box at <u>"+e+"</u> in a moment to complete the sign up process by <b>activating your account</b>.<br><br>You will not be able to do anything on the site until you successfully activate your account.<br></p>";
}
}
}
ajax.send("u="+u+"&e="+e+"&p="+p1+"&c="+c);
}
}
/* function addEvents(){
_("elemID").addEventListener("click", func, false);
}
window.onload = addEvents; */
</script>
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../assets/js/jquery.js"></script>
<script src="../assets/js/bootstrap-transition.js"></script>
<script src="../assets/js/bootstrap-alert.js"></script>
<script src="../assets/js/bootstrap-modal.js"></script>
<script src="../assets/js/bootstrap-dropdown.js"></script>
<script src="../assets/js/bootstrap-scrollspy.js"></script>
<script src="../assets/js/bootstrap-tab.js"></script>
<script src="../assets/js/bootstrap-tooltip.js"></script>
<script src="../assets/js/bootstrap-popover.js"></script>
<script src="../assets/js/bootstrap-button.js"></script>
<script src="../assets/js/bootstrap-collapse.js"></script>
<script src="../assets/js/bootstrap-carousel.js"></script>
<script src="../assets/js/bootstrap-typeahead.js"></script>
<!-- main js -->
<script src="../assets/js/main.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">
//<![CDATA[
$(window).load(function(){
$("form").validate({
rules: {
username: {minlength: 3, required: true, remote: {url: "./validation/check_username.php", type : "get"}},
email: {email: true, required: true, remote: {url: "./validation/check_email.php", type : "get"}},
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.", remote: "This username is taken."},
email: {required: "You need to enter an Email Address.", remote: "This email address is already registered."},
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\" id = 'duplicate_content'><p></p></div></div></div>"
});
_popover.data("popover").options.content = value.message;
return $(value.element).popover("show");
});
}
});
});//]]>
</script>
</head>
<body>
<div id="pageMiddle">
<form novalidate='novalidate' class='form-signin' action='../user/register.php' method='post' name="signupform" id="signupform" onsubmit="return false;">
<img style='margin: -80px 0 0 -135px' src='../assets/img/logo.png' />
<h2 class='form-signin-heading'>Register</h2>
<p>Please fill in your details below to register.</p>
<input name="username" id="username" class='input-block-level' type="text" onblur="checkusername()" onkeyup="restrict('username')" maxlength="16" placeholder="Username">
<input name="email" id="email" class='input-block-level' type="text" onfocus="emptyElement('status')" onkeyup="restrict('email')" maxlength="88" placeholder="Email">
<input name="pass1" id="pass1" class='input-block-level' type="password" onfocus="emptyElement('status')" maxlength="16" placeholder="Create Password">
<input name="pass2" id="pass2" class='input-block-level' type="password" onfocus="emptyElement('status')" maxlength="16" placeholder="Confirm Password">
<select name="country" id="country" onfocus="emptyElement('status')">
<option value="" class="country-default">Select Country</option>
<?php include_once("../php_includes/template_country_list.php"); ?>
</select>
<div style='margin-top: 30px'>
<label class="checkbox inline">
<input type="checkbox" id="tandc" name="tandc" value="agree" /> I agree to the Terms and Conditions.
</label>
</div>
<br /><br />
<button class='btn btn-large btn-primary' id="signupbtn" onclick="signup()">Register</button>
<span id="status"></span>
</form>
</div>
</body>
</html>
如果有人能告诉我应该做些什么,那就太棒了。
提前致谢, 一个Noob