php jq 提交表单,使用jQuery和PHP序列化并提交表单

使用jQuery和PHP序列化并提交表单

我正在尝试使用jQuery发送表单数据。 但是,数据无法到达服务器。 你能告诉我我做错了吗?

我的HTML表单:

JavaScript(与上述表单位于同一文件中):

$(document).ready(function(e) {

$("#contactForm").submit(function() {

$.post("getcontact.php", $("#contactForm").serialize())

// Serialization looks good: name=textInNameInput&&telefon=textInPhoneInput etc

.done(function(data) {

if (data.trim().length > 0) {

$("#sent").text("Error");

} else {

$("#sent").text("Success");

}

});

return false;

})

});

服务器端PHP(/getcontact.php):

$nume = $_REQUEST["nume"]; // $nume contains no data. Also tried $_POST

$email = $_REQUEST["email"];

$telefon = $_REQUEST["telefon"];

$comentarii = $_REQUEST["comentarii"];

你能告诉我我在做什么错吗?

更新

检查var_dump($_POST),它返回一个空数组。

奇怪的是,在我的本地计算机上测试的相同代码可以正常工作。如果我将文件上传到托管空间,它将停止工作。 我尝试在不使用jQuery的情况下制作老式表格,并且所有数据都是正确的。

我看不到这将是服务器配置问题。 有任何想法吗?

谢谢!

9个解决方案

93 votes

您可以使用此功能

var datastring = $("#contactForm").serialize();

$.ajax({

type: "POST",

url: "your url.php",

data: datastring,

dataType: "json",

success: function(data) {

//var obj = jQuery.parseJSON(data); if the dataType is not specified as json uncomment this

// do what ever you want with the server response

},

error: function() {

alert('error handling here');

}

});

返回类型为json

编辑:我使用cache:来防止浏览器在这种情况下提交。

向答案中添加更多数据。

cache:(如果它是跨域调用)。

cache: //这是一个请求前回调函数

cache: //请求结束后要调用的函数。因此无论成功或出错都必须执行的代码可以在此处

cache: //默认情况下,所有请求都是异步发送的

cache: //默认情况下为true。 如果设置为false,将强制浏览器不缓存请求的页面。

在这里找到官方页面

zamil answered 2020-07-18T01:11:19Z

29 votes

您可以在表单数据中添加额外的数据

使用serializeArray并添加其他数据:

var data = $('#myForm').serializeArray();

data.push({name: 'tienn2t', value: 'love'});

$.ajax({

type: "POST",

url: "your url.php",

data: data,

dataType: "json",

success: function(data) {

//var obj = jQuery.parseJSON(data); if the dataType is not specified as json uncomment this

// do what ever you want with the server response

},

error: function() {

alert('error handing here');

}

});

MrBii answered 2020-07-18T01:11:43Z

0 votes

您是否在控制台中检查了来自表单的数据是否已正确序列化? ajax请求成功吗? 另外,您也没有关闭占位符引号,这可能会导致一些问题:

$.post(url, $.param($(this).serializeArray()), function(data) {

});

});

rahulbhondave answered 2020-07-18T01:13:58Z

0 votes

两个结束注册或用户通过表单提交完成注册后,由ajax自动注册到“ Shouttoday”。

var deffered = $.ajax({

url:"code.shouttoday.com/ajax_registration",

type:"POST",

data: $('form').serialize()

});

$(function(){

var form;

$('form').submit( function(event) {

var formId = $(this).attr("id");

form = this;

event.preventDefault();

deffered.done(function(response){

alert($('form').serializeArray());alert(response);

alert("success");

alert('Submitting');

form.submit();

})

.error(function(){

alert(JSON.stringify($('form').serializeArray()).toString());

alert("error");

form.submit();

});

});

});

Zigri2612 answered 2020-07-18T01:14:18Z

0 votes

$(document).ready(

function() {

populateUser();

$("#user").submit(

function(e) {

e.preventDefault();

jQuery.ajaxSetup({

async : false

});

if ($("#roleId").val() != 'Select role') {

$.post($(this).attr("action"), $(this)

.serialize(), function(response) {

alert(response.message);

$("#user")[0].reset();

populateUser();

jQuery.ajaxSetup({

async : true

});

});

} else {

alert("Please Select the role of user");

}

})

});

Aniket answered 2020-07-18T01:14:33Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值