尝试将对象的javascript数组发送到我的另一个php文件,在那里我可以解码它并将元素保存到我的数据库中
嗨,我到处都搜索过了,虽然一些问题和答案对我有所帮助,但我似乎仍然无法将我的对象数组发送到我的PHP文件(body_parts_submit.PHP) 基本上,我已经创建了6个数组,其中有复杂的对象,但我希望每个身体部位、每个练习、每个集合、重复和权重值都不同,以便以后我可以区分它们。 当我在浏览器中运行代码时,success函数似乎从未运行过,因此它可能是我构建数组或javascript本身的方式。任何帮助都将不胜感激。我已经在这个问题上坐了好几天了尝试将对象的javascript数组发送到我的另一个php文件,在那里我可以解码它并将元素保存到我的数据库中,javascript,php,arrays,ajax,object,Javascript,Php,Arrays,Ajax,Object,嗨,我到处都搜索过了,虽然一些问题和答案对我有所帮助,但我似乎仍然无法将我的对象数组发送到我的PHP文件(body_parts_submit.PHP) 基本上,我已经创建了6个数组,其中有复杂的对象,但我希望每个身体部位、每个练习、每个集合、重复和权重值都不同,以便以后我可以区分它们。 当我在浏览器中运行代码时,success函数似乎从未运行过,因此它可能是我构建数组或javascript本身的方式。任何帮助都将不胜感激。我已经在这个问题上坐了好几天了 $("#add-cart").click(
$("#add-cart").click(function(){
var numBodyParts = parseInt($("input[name='number_bodypart']").val());
var bodyParts = new Array();
bodyParts[0] = "Body Parts:";
var exerciseNames = new Array();
exerciseNames[0] = new Array();
exerciseNames[0][0] = "Exercise names:";
var numOfSets = new Array();
numOfSets[0] = new Array();
numOfSets[0][0] = "Number of sets per exercise:";
var setNum = new Array();
setNum[0] = new Array();
setNum[0][0] = new Array();
setNum[0][0][0] = "Set Number:";
var repValue = new Array();
repValue[0] = new Array();
repValue[0][0] = new Array();
repValue[0][0][0] = "Number of reps:";
var weightValue = new Array();
weightValue[0] = new Array();
weightValue[0][0] = new Array();
weightValue[0][0][0] = "Amount of weight:";
for(b=1;b<=numBodyParts;b++){
bodyParts[b] = {bodypart:document.getElementById("bodypart_select"+b).value};
var numExercises = document.getElementById("table"+b).rows.length;
exerciseNames[b] = new Array();
numOfSets[b] = new Array();
setNum[b] = new Array();
repValue[b] = new Array();
weightValue[b] = new Array();
for(e = 1; e<=numExercises;e++){
exerciseNames[b][e] = {exercise:document.getElementById("exercise_"+b+"_"+e).value};
numOfSets[b][e] = {numofsets:document.getElementById("number_set_"+b+"_"+e).value};
var numofSets = document.getElementById("set_"+b+"_"+e).rows.length;
setNum[b][e] = new Array();
repValue[b][e] = new Array();
weightValue[b][e] = new Array();
for(s=1; s<=numofSets; s++){
setNum[b][e][s] = {setnum:s};
repValue[b][e][s] = {reps:document.getElementById(b+"_reps_"+e+"_"+s).value};
weightValue[b][e][s] = {weight:document.getElementById(b+"_weight_"+e+"_"+s).value};
}
}
}
var test1 = JSON.stringify(bodyParts[1]);
var test2 = JSON.stringify(exerciseNames);
var test3 = JSON.stringify(numOfSets);
var test4 = JSON.stringify(setNum);
var test5 = JSON.stringify(repValue[1][2][2]);
var test6 = JSON.stringify(weightValue[1][1][1]);
console.log(test1);
console.log(test2);
console.log(test3);
console.log(test4);
console.log(test5);
console.log(test6);
$.ajax({
type: "post",
url: "body_parts_submit.php",
data: {bodyparts:JSON.stringify(bodyParts[1])},
success: function(data){
alert("Success");
console.log(data);
},
error: function(e){
console.log(e.message);
},
dataType: "json"
});
});
当您不发送任何内容或硬编码数据进行测试时,它是否运行?post请求是否执行并且
body\u parts\u submit.php
是否接收任何内容?print\r($data)
你不能回显它。只需将你所有的数组放入一个var bigArray
并传递数据:bigArray,
和jQuery将为你在PHP中完成所有繁重的工作做一个文件放置内容('param.txt',print_r($\u POST,1))
它将向您显示$\u POST
中的数据到达PHP脚本时的样子
$data = json_decode($_POST["bodyparts"]);
if(isset($_POST["bodyparts"]))
{
$data = json_decode($_POST["bodyparts"]);
echo('{"reason": "'.$data.'"}');
}
else{
echo ('{"reason": "failure"}');
}