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!

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度