Jquery表单提交仍在刷新


Jquery form submission still refreshing

我正试图将ajax表单提交添加到我正在使用jquery开发的PHP web应用程序中。表单正在提交并写入数据库,但它仍然会在刷新后完成所有操作。

这是我的代码:

$("form#form_customer").submit(function() {
            var customer123first_name = $('input[name=customer123first_name.]');
            var customer123last_name = $('input[name=customer123last_name.]');
            var customer123date_of_birth = $('input[name=customer123date_of_birth.]');
            var customer123email_address = $('input[name=customer123email_address.]');
            var customer123telephone_number = $('input[name=customer123telephone_number.]');
            var customer123picture = $('input[name=customer123picture.]');
            var customer123id_picture = $('input[name=customer123id_picture.]');
            var customer123id_expiration = $('input[name=customer123id_expiration.]');
            var data = 'customer123first_name=' + customer123first_name.val() + '&customer123last_name=' + customer123last_name.val() + '&customer123date_of_birth=' + customer123date_of_birth.val() + '&customer123email_address=' + customer123email_address.val() + '&customer123telephone_number=' + customer123telephone_number.val() + '&customer123picture=' + customer123picture.val() + '&customer123id_picture=' + customer123id_picture.val() + '&customer123id_expiration=' + customer123id_expiration.val();
            $.ajax({
                url: "inc/createObject.php",
                type: "POST",
                data: data,
                cache: false,
                success: function(data){
                    $('form_success').fadeIn();
                }
            });
            return false;
        });

我在网上读到的关于这个特定问题的所有内容都发现return false;调用出现在错误的位置,或者根本不存在。我已经检查过我的放在正确的地方了,我就是找不到它令人耳目一新的原因。

我知道jquery正在工作,因为我用它来做弹出窗口,这些窗口运行得很好。

如果您想在上下文中查看代码,请访问www.sfyfe.com/studioadmin

问题是由$('input[name=customer123...行上选择器末尾的点引起的。点没有做任何事情,它使选择器无效。删除这些点应该可以解决问题。希望这能有所帮助!

也许

$("form#form_customer").submit(function(e) {
  e.preventDefault();
});

您应该以JSON格式发送数据,使用:"data:data"无效,尝试:

   $.ajax({
        url: "inc/createObject.php",
        type: "POST",
        data:{dataField : data },

您的代码应该是这样的。

$("form#form_customer").submit(function() {
            var customer123first_name = $('input[name=customer123first_name]');
            var customer123last_name = $('input[name=customer123last_name]');
            var customer123date_of_birth = $('input[name=customer123date_of_birth]');
            var customer123email_address = $('input[name=customer123email_address]');
            var customer123telephone_number = $('input[name=customer123telephone_number]');
            var customer123picture = $('input[name=customer123picture]');
            var customer123id_picture = $('input[name=customer123id_picture]');
            var customer123id_expiration = $('input[name=customer123id_expiration]');
            var data = 'customer123first_name=' + customer123first_name.val() + '&customer123last_name=' + customer123last_name.val() + '&customer123date_of_birth=' + customer123date_of_birth.val() + '&customer123email_address=' + customer123email_address.val() + '&customer123telephone_number=' + customer123telephone_number.val() + '&customer123picture=' + customer123picture.val() + '&customer123id_picture=' + customer123id_picture.val() + '&customer123id_expiration=' + customer123id_expiration.val();
            $.ajax({
                url: "inc/createObject.php",
                type: "POST",
                data: data,
                cache: false,
                success: function(data){
                    $('form_success').fadeIn();
                }
            });
            return false;
        });