How to get data in php from $.post into $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 form...

问题:

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:

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!

  • 发表于 2019-03-03 18:00
  • 阅读 ( 248 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除