1

私は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]

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 に答える