1

我是 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]

4

4 回答 4

1

user_email_exists进行异步 Ajax 调用时,该调用不可能像编写函数那样向服务器返回值。

于 2012-08-10T14:26:09.803 回答
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;

    });

});
于 2012-08-10T14:30:14.100 回答
0

正如您在上面的评论中指出的那样,重定向不起作用。

这就是为什么...

window.location.replace('/forums/usercp.php');

应该...

window.location.replace('forums/usercp.php');

当您可能需要相对路径时,您就有了绝对路径。

于 2012-08-10T14:30:04.387 回答
-2

在代码的不同行中添加一些console.log(response)或警报并验证您的控制台

于 2012-08-10T14:40:21.017 回答