Javascript Jquery AJAX未通过";邮政「;数据
我有一个带有输入字段的简单div,用于插入团队数据的数据Javascript Jquery AJAX未通过";邮政「;数据,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个带有输入字段的简单div,用于插入团队数据的数据 <div> <label>Register</label> <input type="text" name="nt" placeholder="Team Name"> <label>Team</label> <input type="text" name="n1" placeholder="Steam us
<div>
<label>Register</label>
<input type="text" name="nt" placeholder="Team Name">
<label>Team</label>
<input type="text" name="n1" placeholder="Steam username">
<input type="text" name="n2" placeholder="Steam username">
<input type="text" name="n3" placeholder="Steam username">
<input type="text" name="n4" placeholder="Steam username">
<input type="submit" id="sbutton"></input>
</div>
在我的php文件中,我处理这些数据,但问题是,jquery函数没有传递这些数据。我的php:
<?php
session_start();
include_once "functions.php";
print_r($_POST);
if(!isset($_POST["nt"])) die("Usernames not set");?>
为什么会发生这种情况?只要更换即可
// dictionary, i.e. sdata["nt"] would not work
var sdata=[];
借
JQuery对传递的数据使用JSON.stringify方法
如果您有疑问,请尝试使用
console.log(JSON.stringify(sdata))
更改为object并将object的属性名设置为input,以使用当前使用的相同php方法
$('#sbutton').click(function(){
var sdata={};
$(this).parent().children("input[type=text]").each(function(index,value){
sdata[this.name]=this.value;
});
console.log(sdata);
$.post('php/team.php',sdata,
function(returnedData){
console.log(returnedData);
}).fail(function(){alert('Ooops something wrong')});
});
通过返回完整的$\u POST
可以很容易地看到接收到的内容,它将出现在成功处理程序的控制台日志中
echo json_encode($_POST);exit;
您也可以像这样使用FormData对象,即使您的HTML中似乎没有定义
FormData对象允许您编译一组键/值对,以便使用XMLHttpRequest发送。它主要用于发送表单数据,但可以独立于表单使用,以便传输键控数据。如果表单的编码类型设置为multipart/form data,则发送的数据的格式与表单的submit()方法发送数据的格式相同
console.log(JSON.stringify(sdata))
$('#sbutton').click(function(){
var sdata={};
$(this).parent().children("input[type=text]").each(function(index,value){
sdata[this.name]=this.value;
});
console.log(sdata);
$.post('php/team.php',sdata,
function(returnedData){
console.log(returnedData);
}).fail(function(){alert('Ooops something wrong')});
});
echo json_encode($_POST);exit;
$('#sbutton').click(function(){
var formData = new FormData();
$(this).parent().children("input[type=text]").each(function(index,value){
formDate.append( $(this).attr("name"), $(this).val() );
});
$.post('php/team.php',formData,
function(returnedData){
console.log(returnedData);
})
.fail(function(){alert('Ooops something wrong')});
});