我正在尝试将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
答案 0 :(得分:1)
也许
$("form#form_customer").submit(function(e) {
e.preventDefault();
});
答案 1 :(得分:1)
问题是由$('input[name=customer123...
行上选择器末尾的点引起的。点没有做任何事情,它使选择器无效。删除点应该可以解决问题。希望这有帮助!
答案 2 :(得分:0)
您应该以JSON格式发送数据,使用:“data:data”无效, 试试:
$.ajax({
url: "inc/createObject.php",
type: "POST",
data:{dataField : data },
答案 3 :(得分:0)
您的代码应该是这样的。
$("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;
});