dryb38654 2019-02-26 22:05
浏览 103

如何将PHP中的数据从$ .post转换为$ variable?

I have found a lot of answers to this matter, but did not found working solution for my form posting with $.post to a php file which stores data into a csv file.

The formData gets serialized in another function which stores the data in a const for storing localy. The actually $.post gets done in this part of code.

jQuery code:

function submit(formData) {

   function passVal(){

       $.post(
           "post.php",
           formData,
           function(formData,status,xhr){

           }
       );

    return false;

   }
    passVal();


}

PHP Code:

$obj = $_POST;
$retailer = $obj['retailer'];

$question1  = $obj['question1'];
$offline = $obj['offline'];
$question2_1 = $obj['question2_1'];
$question2_2 = $obj['question2_2'];
$question2_3 = $obj['question2_3'];
$question3 = $obj['question3'];
$timestamp = $formdata['timestamp'];

$old_file = fopen('/output/results.csv', 'a');


$list = array
(
    $retailer . ';' . $question1  . ';' . $question2_1  . ';' . $question2_2  . ';' . $question2_3  . ';' . $question3 . ';' . $offline . ';' .$timestamp


);
fputcsv($old_file, $list);

fclose($old_file);

Is there a working solution? These answers: Can't retrieve jQuery serialized form data with PHP $_POST[] variable Do not work for me.

I wanted to get the data into the different $vars in php?

  • 写回答

1条回答 默认 最新

  • du521521521 2019-02-27 08:40
    关注

    With help from Xatenev, I found the next solution:

    $obj = $_POST['data'];
    parse_str($obj, $output);
    $question1 = $output['question1'];
    

    $obj = $_POST['data'] will give you a string: question1=Yes&question2=No

    parse_str() will parse string into variables. And now it can be used in my php file http://www.php.net/manual/en/function.parse-str.php

    tnx community for the quick help&response!

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器