我有一个名为my-form的表单。它的内部有2个文本框和1个多选标记。我无法在PHP中获取所有传递的数据。
textbox1的名称为“ tname”
textbox2的名称为“ tcode”
多重选择的名称为“ tsubteam”
我能够传递所有数据,但是我不知道如何在PHP中获取所有数据
var arr = [];
$('#tsubteam > option').each(function(){
arr.push(this.value +","+this.text)
});
$.ajax({
type: "POST",
url: "modify_team.php",
dataType: "json",
contentType: "application/x-www-form-urlencoded; charset=UTF-8;",
data: `$('#my-form').serialize()+"&id="+getQueryVariable('id')+"&subteam="+JSON.stringify(arr),
cache: false,
success: function(data)
{
console.log(data)
},
error: function(xhr, ajaxoption, thrownerror)
{
alert(xhr.status+" "+thrownerror);
}
在PHP中:
<?php
$tcode = $_POST['tcode'];
$tname = $_POST['tname'];
$id = $_POST['id'];
$subteam = $_POST['subteam'];
?>
答案 0 :(得分:0)
我已经在这里解决了主要问题。
我所做的就是更改
dataType: "json"
进入
dataType: "text"
并将每个选项标签的检索代码修改为
$('#tsubteam > option').each(function(){
item = {}
item["id"] = this.value
item["name"] = this.text
arr.push(item)
});
在PHP中:
$tcode = $_POST['tcode'];
$tname = $_POST['tname'];
$id = $_POST['id'];
$subteam = json_decode($_POST['subteam'], true);
foreach($subteam as $k=>$v)
{
$subteam_name = $v["name"];
$subteam_id = $v["id"];
}
在AJAX中传递的数据的输出
tcode=CMPSS&tname=Compass&id=1&subteam=[{"id":"1","name":"Compass Team A"},{"id":"2","name":"Compass Team B"},{"id":"3","name":"Falcon"},{"id":"4","name":"GPON"},{"id":"5","name":"TTV"},{"id":"6","name":"INFRA"},{"id":"7","name":"OSS"}]