0

我在我的网站上有一个注册表单,在我的用户注册并创建一个新帐户后,该表单不会进入success函数内部,但信息会正确保存在数据库中。

ajax函数是:

$(document).ready(function(){
    //To validate the registration form and save its value after validation
    $('#registerForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            email: {
                validators: {
                    notEmpty: {
                            message: 'The email is required and cannot be empty'
                    },
                    emailAddress: {
                        message: 'The input is not a valid email address'
                    }
                }
            }
        }
    })
    .on('success.form.bv', function(e) {
            // Prevent form submission
            e.preventDefault();

            // Get the form instance
            var $form = $(e.target);

            // Get the BootstrapValidator instance
            var bv = $form.data('bootstrapValidator');

            // Use Ajax to submit form data
           $.ajax({
                        type: $form.attr('type'),
                        url: $form.attr('action'),
                        data: $(form).serialize(),
                        dataType: json,
                        success: function(msg) {
                                alert(msg);
                        }
                    });
        });
}); 

形式的php函数action

<?php
$server_root ="../";
include_once("{$server_root}include-structure/ajax_header.php");

$first_name = mysql_real_escape_string($_POST['firstName']);
$last_name = mysql_real_escape_string($_POST['lastName']);
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$email = mysql_real_escape_string($_POST['email']);
$phone = mysql_real_escape_string($_POST['phone']);
function better_crypt($input, $rounds = 7)
{
    $salt = "";
    $salt_chars = array_merge(range('A','Z'), range('a','z'), range(0,9));
    for($i=0; $i < 22; $i++) 
    {
        $salt .= $salt_chars[array_rand($salt_chars)];
    }
    return crypt($input, sprintf('$2a$%02d$', $rounds) . $salt);
}

$password_hash = better_crypt($password);

$join_date = date('Y-m-d H:i:s');

$sql = $GLOBALS['db1']->query("INSERT INTO users (first_name, last_name, username, password, email, mobile, user_type, account_type, join_date) VALUES ('$first_name','$last_name','$username','$password_hash','$email',$phone,1,1,'$join_date')");

if($sql)
{
    $msg = 'Success';
}
echo json_encode($msg);
?>

提交并存储信息后,我$msg在屏幕上打印而不是将其作为响应返回到 ajax,并且我的用户也重定向到我的 PHP 文件的 URL 而不是我的注册页面 URL。

为什么代码没有进入成功函数内部?

4

0 回答 0