对PHP文件的AJAX调用没有响应?

时间:2016-12-22 16:15:03

标签: javascript php jquery ajax

我有一个非常奇怪的问题。我有以下形式,它存在于一个模态中:

<div id="regModal" class="modal regModal fade" role="dialog">
<div class="modal-dialog">


    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h2 class="modal-title text-center">Register</h2>

        </div>
        <form name='user-signup' method='post' id='user-signup'>
            <div class="modal-body">
                <div id="modal-form-main-reg" class="modal-form-main">
                    <div class="error"></div>
                    <div class="form-group">
                        <input class="form-control input-lg capt" id="f-name-modal" name="f-name"
                               placeholder="First Name"
                               type="text" required="required" spellcheck="false" maxlength="12"/>
                        <input class="form-control input-lg capt" id="l-name-modal" name="l-name" placeholder="Last Name"
                               type="text" required="required" spellcheck="false" maxlength="12"/>
                    </div>

                    <div class="form-group">
                        <input class="form-control input-lg" id="pwd_modal" name="pwd_modal"
                               placeholder="Password"
                               type="password" required="required" spellcheck="false" maxlength="12"/>
                        <input class="form-control input-lg" id="pwd_confirm_modal" name="pwd_confirm_modal"
                               placeholder="Confirm Password"
                               type="password" required="required" spellcheck="false" maxlength="12"/>
                    </div>

                    <div class="form-group">
                        <input class="form-control input-lg" id="subject" name="subject"
                               placeholder="Email address" type="text" required="required"
                               spellcheck="false" maxlength="30"/>
                        <input class="form-control input-lg" id="phone_no" name="phone_no"
                               placeholder="Phone No" type="text" required="required"
                               spellcheck="false" maxlength="14"/>
                    </div>
                </div>

                    <div class="checkbox">
                        <label>
                            <input id="reg_worker_check" type="checkbox" value="">
                            <span class="cr"><i class="cr-icon glyphicon glyphicon-ok"></i></span>
                            I'm registering as a (SP)
                        </label>
                    </div>
                </div>

                <div class="form-group text-center">
                    <button id='usr_signup_btn' type="submit" class="btn btn-success btn-lg btn-next">CONTINUE</button>
                </div>
                <span class="reg-note">By registering, you agree to our <a href="#">Terms and Conditions</a>.</span>

            </div>
            <div class="modal-footer">
                <span class="ret-user">Returning user? <a href="/invercer/login">Login</a></span><span
                    class="need-help"><a
                        href="/invercer/blog">Need Help?</a></span>
            </div>
        </form>

        <script>
            $('#reg_worker_check').change(function(){
                if ($(this).is(':checked')) {
                    $('form#user-signup').attr('action', 'signup').attr('method', 'get');
                } else {
                    $('form#user-signup').attr('action', '').attr('method', 'post');
                }
            });

            $("form#user-signup input").on("keydown", function (e) {
                return e.which !== 32;
            });

            $('form#user-signup input.capt').on('keydown', function(e) {
                if (this.value == '') {
                    var char = String.fromCharCode(e.which);
                    if (char.match(/^\w$/)) {
                        // If is empty and we pressed a printable key...
                        this.value = char.toUpperCase();
                        return false;
                    }
                }
            });
        </script>
    </div>

</div>

我有以下JS文件(外部链接),其中包含一些jquery验证,并且应该序列化从表单发布的内容并发送到“user-reg.php”

$(document).ready(function() {
/* validation */
$("#user-signup").validate({
    rules : {
        pwd_modal: {
            minlength: 6
        },
        pwd_confirm_modal: {
            minlength: 6,
            equalTo: "#pwd_modal"
        },
        subject: {
            required: true,
            email: true
        },
        submitHandler: submitForm
    }
});

function submitForm() {
    var data = $("#user-signup").serialize();

    $.ajax({
        type : 'POST',
        url  : 'user-reg.php',
        data : data,
        beforeSend: function() {
            $("#error").fadeOut();
            $("#usr_signup_btn").html("<span class='preloader-wrap'><img src='/invercer/img/preloader/103.gif' width='15' height='10' /></span> &nbsp; VERIFYING DATA ...");
        },
        success :  function(response) {
            if(response=="ok"){
                $("#usr_signup_btn").html("<span class='preloader-wrap'><img src='/invercer/img/preloader/103.gif' width='15' height='10' /></span> &nbsp; ADDING USER ...");
                setTimeout(function() {
                    $('#modal-form-main-reg').fadeOut('fast', function() {
                        $('#modal-form-main-reg').replace('<div class="modal-form-success-main text-center"><span>Registration successful!</span><br><br><a href="/login"><button class="form-control input-md">LOGIN PLEASE</button></a></div>').fadeIn('fast');
                    })
                }, 4000);
            }

            else if(response=="err") {
                $("#error").fadeIn(1000, function(){
                    $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; Registration error. Try again.</div>');
                        $("#usr_signup_btn").html('CONTINUE');
                });
            }

            else if(response=="crosscheck") {
                $("#error").fadeIn(1000, function(){
                    $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; Error. Please crosscheck what you typed.</div>');
                    $("#usr_signup_btn").html('CONTINUE');
                });
            }

            else{
                $("#error").fadeIn(1000, function(){
                    $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; '+response+' Unknown error!</div>');
                    $("#usr_signup_btn").html('CONTINUE');
                });
            }
        },
        error: function(response) {
            $("#error").fadeIn(1000, function(){
                $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; '+response+' Unknown error!</div>');
                $("#usr_signup_btn").html('CONTINUE');
            });
        }
    });
    return false;
}

});

在user-reg.php中,我有一系列数据库调用和定期检查:

            <?php
            ob_start();

            session_start();

            error_reporting(E_ALL | E_STRICT);

            require('app/functions.php');

            if(isset($_SESSION['user']) || isset($_SESSION['artisan'])) {
                header("Location: https://blabla.com");
            }

            else {

                if (isset($_POST['f-name']) && isset($_POST['l-name']) && (!isset($_SESSION['user']) && !isset($_SESSION['artisan']))) {
                    $f_name = sanitizeString($db, $_POST['f-name']);
                    $l_name = sanitizeString($db, $_POST['l-name']);
                    $email = sanitizeString($db, $_POST['subject']);
                    $tel = sanitizeString($db, $_POST['phone_no']);
                    $date = date('m-d-Y');

                    $key = true;
                    $usr_name = '';

                    while($key){
                        $usr_name = mt_rand(100000, 999999);
                        if(!compare_u($db, $usr_name)) $key = false;
                    }

                    $password = sanitizeString($db, $_POST['pwd_modal']);
                    $encrypted_password = encryptIt($password);

                    $query_sign_up = queryMysql($db, "INSERT INTO users VALUES('$usr_name', '$encrypted_password', '$f_name', '$l_name', '$email', '$tel', NULL, '$date')");
                    if($query_sign_up) echo "ok";
                    else echo "err";
                }
                else echo "crosscheck";
            }

问题是,无论何时我发布表单,它都不会从user-reg.php收到任何内容。它似乎甚至没有看到脚本。因此,无论何时我发布表单,它都会重新加载同一页面,而不是通过AJAX接收ANYTHING。在JS文件上,它可以使用验证部分,仅此而已。这真的很有趣,因为我有另一个登录脚本使用相同的技术完美地工作。 Validate.js和其他脚本正确包含在标题中。

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

可能没有执行submitForm,请尝试submitForm()