1

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?

Sjoerd
  • 182
  • 1
  • 14
  • 3
    I don't understand.. what is your exact problem with this code now? Do you get any error? Whats happening? – Xatenev Feb 26 '19 at 22:06
  • The vars do not get filled.... they are empty – Sjoerd Feb 26 '19 at 22:07
  • 1
    I see, have you investigated the ajax call in the browser dev tools network tab? Are these fields: 'question1' 'offline' etc. actually sent? Have you tried `var_dump($_POST);`? – Xatenev Feb 26 '19 at 22:08
  • yes they are... and if I store them in a log file... the entire string is there – Sjoerd Feb 26 '19 at 22:09
  • ?? "if I store them in a log file"? How can they be empty if you can store them in a log file – Xatenev Feb 26 '19 at 22:10
  • the entire string is stored in log file... but parts of the string I cannot seperate – Sjoerd Feb 26 '19 at 22:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189094/discussion-between-xatenev-and-sjoerd). – Xatenev Feb 26 '19 at 22:12
  • I recommend posting and accepting your own answer, based on your findings in chat. – showdev Feb 27 '19 at 02:10

1 Answers1

0

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!

Sjoerd
  • 182
  • 1
  • 14