2个单独的$ _POST发送到同一个文件

时间:2015-07-20 22:19:10

标签: javascript php json post

我正在制作表单,填写完成后,会将所有项目发送到帖子中。 Json编码它看起来像这样。

{
    "encounterName":"Violence",
    "encounterHint":"",
    "basicTrainingSectionSlide":["1","6"],
    "basicTrainingContentHeader":["2","4"],
    "basicTrainingContentText":["3","5"],
    "contextText_1":"",
    "contextText_2":"",
    "contextText_3":"",
    "contextText_4":"",
    "contextText_5":"",
    "submit_form":"Submit"
}

但是,.js文件中有一个特定的数组,在表单提交时最终确定。它基本上跟踪按哪个按钮按顺序,所以我可以按顺序吐出信息。该功能看起来像这样。

function returnValues() {
    $.ajax({
        type: "POST",
        url: "final.php",
        data:{ Sections: $sectionOrder }, 
        success: function(data){
            console.log(data); 
        }
    })
}

然而现在,当我对json进行编码时,我得到了这个:

{
    "encounterName":"Violence",
    "encounterHint":"",
    "basicTrainingSectionSlide":["1","6"],
    "basicTrainingContentHeader":["2","4"],
    "basicTrainingContentText":["3","5"],
    "contextText_1":"",
    "contextText_2":"",
    "contextText_3":"",
    "contextText_4":"",
    "contextText_5":"",
    "submit_form":"Submit"
}

{
    "Sections":["","Section","Text","Text","Section"]
}

{
    "Sections":["","Section","Text","Text","Section"]
}

此外,一旦post的部分内容被调用到foreach循环中:

foreach($_POST['Sections'] as $order){
        file_put_contents($_POST['encounterName'].".json", "\t\t\t\"".$_POST['basicTrainingSectionSlide'][0]."\"\n", FILE_APPEND);
}

$_POST['encounterName']最终等于“.json”,$_POST['basicTrainingSectionSlide'][0]变为空字符串,直到循环完成。

这就是var_dump返回的内容:

array(11) { ["encounterName"]=> string(8) "Violence" ["encounterHint"]=> string(0) "" ["basicTrainingSectionSlide"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "6" } ["basicTrainingContentHeader"]=> array(2) { [0]=> string(1) "2" [1]=> string(1) "4" } ["basicTrainingContentText"]=> array(2) { [0]=> string(1) "3" [1]=> string(1) "5" } ["contextText_1"]=> string(0) "" ["contextText_2"]=> string(0) "" ["contextText_3"]=> string(0) "" ["contextText_4"]=> string(0) "" ["contextText_5"]=> string(0) "" ["submit_form"]=> string(6) "Submit" } array(11) { ["encounterName"]=> string(8) "Violence" ["encounterHint"]=> string(0) "" ["basicTrainingSectionSlide"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "6" } ["basicTrainingContentHeader"]=> array(2) { [0]=> string(1) "2" [1]=> string(1) "4" } ["basicTrainingContentText"]=> array(2) { [0]=> string(1) "3" [1]=> string(1) "5" } ["contextText_1"]=> string(0) "" ["contextText_2"]=> string(0) "" ["contextText_3"]=> string(0) "" ["contextText_4"]=> string(0) "" ["contextText_5"]=> string(0) "" ["submit_form"]=> string(6) "Submit" }

我需要那个循环中的信息。

0 个答案:

没有答案