Php 如何强制ajax函数进入success函数?

Php 如何强制ajax函数进入success函数?,php,jquery,ajax,twitter-bootstrap,jqbootstrapvalidation,Php,Jquery,Ajax,Twitter Bootstrap,Jqbootstrapvalidation,我使用bootstrap注册表表单获取用户信息并将其存储在我的数据库中。我使用ajaxrequest提交表单信息并在验证后存储 信息经过正确验证,然后正确存储在我的数据库中 问题在于存储信息后,请求不会返回响应 这是验证引导代码和ajax请求: $(document).ready(function(){ //To validate the registration form and save its value after validation $('#registerForm'

我使用bootstrap注册表表单获取用户信息并将其存储在我的数据库中。我使用
ajax
request提交表单信息并在验证后存储

信息经过正确验证,然后正确存储在我的数据库中

问题在于存储信息后,请求不会返回响应

这是验证引导代码和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: "POST",
                url: $form.attr('action'),
                data:$(form).serialize(),
                dataType: "json",
                success: function(data)
                {
                     alert(data);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest);
                        alert(textStatus);
                        alert(errorThrown);
                        alert(XMLHttpRequest.responseText);
               }
            });
        });
}); 
ajax函数没有包含任何
success
error
函数,正如我所说,它工作正常,可以正确存储所有内容,但它不会返回ajax函数,而是在浏览器中打印响应

这也是php页面:

<?PHP
$server_root = "../";
include_once("{$server_root}include-sql/mysql.class.php");
include_once("{$server_root}config.php");
Global $db1;
$db1 = new db_mysql($conf['db_hostname'], $conf['db_username'], $conf['db_password'], $conf['db_name']);
$db1->query("SET NAMES utf8");
date_default_timezone_set('Asia/Istanbul');

$email = mysql_real_escape_string($_POST['email']);

$sql = $GLOBALS['db1']->query("INSERT INTO users (email) VALUES ('$email')");

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


msg
是在提交后打印的,我无法提醒它,因为它没有返回ajax成功功能???这段代码中有什么错误或遗漏了什么吗?

它需要一个json响应。你不会还的$msg是一个字符串,不能编码为json

我会这样做:

$success = false;
if($sql)
{
    $success = true;
}
echo json_encode(array("success" => $success));
然后在你的成功函数中:

alert(data.success);

您以前问过这个问题,您的问题似乎是表单直接通过浏览器默认提交。表单是否由ajax加载,因此在代码运行时不可用?请尝试
alert($('#registerForm').length)
我的表单未由ajax加载。这是html表单,我用这个
jQuery
代码验证它。我停止了默认提交,正如你在代码
e.preventDefault()中看到的那样
当我在加载文档后和提交任何内容之前提醒表单长度时,它会显示
1
,但页面是否重新加载正确?因此出现了一些问题页面重新加载php页面(其中包含保存信息然后返回jason msg的sql查询)而不是注册页面,这意味着
preventDefault
不起作用。最好的猜测是页面中有脚本错误阻止代码运行,或者选择器错误,或者插件的使用不正确它仍然在打印而不是打印
success
现在它打印
{“success”:true}
听起来好像没有阻止正常的发布。url是否更改为您的php页面?将此添加到您的表单链接中,看看是否有帮助:('submit',函数(e){e.preventDefault();})如果您检查表单,您将看到这一行我已经阻止了defualtyou没有阻止submit事件。您正在阻止success.form.bv事件,该事件不同。