我是jQuery的新手,并且在尝试使成功功能正常工作时遇到了一些麻烦。我一直在努力阅读有关同一问题的一些人的问题,并已应用所需的更改,但似乎无法使其正常工作。
如果有人可以请我指出正确的方向。
/**
* Add user
*/
function display_reg_success() {
alert('Display function worked');
window.location.replace('/forums/usercp.php');
}
function add_user(data_string) {
alert('called add user');
$.ajax({
type: 'POST',
url: '/index.php',
data: data_string,
success: function (response) {
display_reg_success();
}
});
}
function user_email_exists(username, email) {
$.get("/verify.php", {
username: username,
email: email
},
function (data) {
alert('Data:' + data);
if (data == 'username') {
$('#username_taken_error').show();
$('#username').focus();
}
else if (data == 'email') {
$('#email_taken_error').show();
$('#email').focus();
}
else if (data == 'username|email') {
$('#username_taken_error').show();
$('#email_taken_error').show();
$('#username').focus();
}
else if (data == 'true') {
alert('true');
return true;
}
});
}
/** Regform - Paul */
$(function () {
$('.submit_reg_field').click(function () {
$('.error_form').hide();
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
//required
var username = $("input#username").val();
var email = $("input#email").val();
var confirm_email = $("input#email2").val();
var password = $("input#password").val();
var password2 = $("input#password2").val();
var agree_terms = $('#agree:checked').val();
/*
//options
var hideemail = $("#hideemail:checked").val();
var receivepms = $("#receivepms:checked").val();
var pmnotice = $("#pmnotice:checked").val();
var emailpmnotify = $("#emailpmnotify:checked").val();
var invisible = $("#invisible:checked").val();
var timezone = $("#timezone option:selected").val();
*/
if (username == '') {
$('#username_error').show();
$('#username').focus();
return false;
}
if (password == '') {
$('#password_error').show();
$('#password').focus();
return false;
}
if (password.length < 6) {
$('#password_length_error').show();
$('#password').focus();
return false;
}
if (password2 == '') {
$('#confirm_password_error').show();
$('#password2').focus();
return false;
}
if (password != password2) {
$('#confirm_password_match_error').show();
$('#password').focus();
return false;
}
if (email == '') {
$('#email_error').show();
$('#email').focus();
return false;
}
else {
if (!emailReg.test(email)) {
$('#email_valid_error').show();
$('#email').focus();
return false;
}
}
if (confirm_email == '') {
$('#confirm_email_error').show();
$('#confirm_email').focus();
return false;
}
if (email != confirm_email) {
$('#confirm_email_match_error').show();
$('#email').focus();
return false;
}
if (agree_terms != "1") {
$('#agree_error').show();
return false;
}
var data_string = 'register=true&username=' + username + '&email=' + email + '&email2=' + confirm_email + '&password=' + password + '&password2=' + password2;
if (user_email_exists(username, email)) {
/*+ '&hideemail=' + hideemail + '&receivepms=' + receivepms + '&pmnotice=' + pmnotice + '&emailnotify=' + emailnotify + '&invisible=' + invisible + '&timezone=' + timezone*/
add_user(data_string);
}
return false;
});
});
表格
<table id="reg_table" cellspacing="10">
<tr>
<td width="50%">Username</td>
<td>
<input type="text" class="text_field" id="username" value="" />
<span class="error_form" id="username_error">This field is required.</span>
<span class="error_form" id="username_taken_error">That username is already in use.</span>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" id="password" class="text_field" value="" />
<span class="error_form" id="password_error">This field is required.</span>
<span class="error_form" id="password_length_error">The password is too short.</span>
<span class="error_form" id="confirm_password_match_error">Passwords do not match.</span>
</td>
</tr>
<tr>
<td>Confirm Password</td>
<td>
<input type="password" id="password2" class="text_field" value="" />
<span class="error_form" id="confirm_password_error">This field is required.</span>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input type="text" id="email" class="text_field" value="" />
<span class="error_form" id="email_error">This field is required.</span>
<span class="error_form" id="email_valid_error">This is not a valid email.</span>
<span class="error_form" id="email_taken_error">That email is already in use.</span>
</td>
</tr>
<tr>
<td>Confirm Email</td>
<td>
<input type="text" id="email2" class="text_field" value="" />
<span class="error_form" id="confirm_email_error">This field is required.</span>
<span class="error_form" id="confirm_email_match_error">Emails do not match.</span>
</td>
</tr>
<tr>
<td colspan="2" align="center">I agree to the
<a href="#">Terms and Conditions</a>
<input type="checkbox" id="agree" value="1" />
<span class="error_form" id="agree_error">You must agree to the Terms and Conditions.</span>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Register" class="submit_reg_field" />
</td>
</tr>
</table>
[/ HTML]
答案 0 :(得分:1)
user_email_exists
进行异步Ajax调用,该调用不可能像编写函数一样将值返回给服务器。
答案 1 :(得分:1)
应该是
window.location = '/forums/usercp.php';
不过,请查看jQuery的一些表单验证插件
编辑:我将展示流程应该如何
/**
* Add user
*/
var display_reg_success = function() { //scoping
console.log('Display function worked'); //console is better debugging tool
window.location.replace('/forums/usercp.php');
}
var add_user = function(data) {
console.log('called add user');
$.ajax({
type: 'POST',
url: '/index.php',
data: data, //see notes on serialization below
success: display_reg_success
});
}
var user_email_exists = function(data) {
$.get("/verify.php", data,
function (response) {
console.log('Response:',response);
if (response== 'username') {
//OMIT
}
else if (response== 'true') {
console.log('true');
add_user(data);
}
});
}
/** Regform - Paul */
$(function () {
$('.submit_reg_field').click(function () {
$('.error_form').hide();
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
//required
var username = $("input#username").val();
var email = $("input#email").val();
var confirm_email = $("input#email2").val();
var password = $("input#password").val();
var password2 = $("input#password2").val();
var agree_terms = $('#agree:checked').val();
//validation
//notes on serialization: better make an object and let jquery serialize it,
//i didnt seen any prevention code from entering an `&` into username field
//and so introduce a security hole
var data = {
'register':true,
'username':username,
'email':email,
'email2':confirm_email,
'password':password
//'password2': password //same is password, omit
};
user_email_exists(data);
return false;
});
});
答案 2 :(得分:0)
正如您在上面的评论中指出的那样,重定向无效。
这就是为什么......
window.location.replace('/forums/usercp.php');
应该是......
window.location.replace('forums/usercp.php');
当你可能想要一个亲戚时,你有一个绝对的路径。
答案 3 :(得分:-2)
在代码中的不同行添加一些console.log(response)
或提醒并验证您的控制台