私はjQueryを初めて使用し、success関数が正しく機能しないようにするのに問題があります。私は同じ問題に関するいくつかの人々の質問を読むために最善を尽くしており、必要な変更を適用しましたが、正しく機能させることができないようです。
誰かが私を正しい方向に向けてくれたら。
/**
* 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]