Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
JS $.post > dump PHP $_POST data to file

I've been trying this for hours and finally give up. As you can tell I've a huge noob and have little to no idea what I'm doing...

I have some JS being called from a button onclick= which POSTs to a PHP file. I'd like to take this POST data and just write it to a file, nothing fancy, just dumping it raw.

I've tried various methods, but none seem to work - either not writing the data at all, or writing "()", "[]" (if trying to encode the POST data as JSON), or just the word "array" and so on.

Methods I've tried;

file_put_contents('test.txt', file_get_contents('php://input'));  //this I thought *should* definitely work...

var_dump / var_export / print_r

I've tried storing the above as $data and writing that as well. Just nothing I do seems to work at all.

I'm mostly trying to use fopen/write/close to do the deed (because that's all I really "know"). File is writable.

(part of the) JS I'm using to POST:

(from button onclick="send('breakfast'))

function send(food){
if(food == 'breakfast'){
    $.post("recorder.php?Aeggs=" + $("textarea[name=eggs]").val());

I'm not looking to extract(?) values from the POST data, just write it "as-is" to a file, and I'm not bothered on the formatting etc.

Would someone please assist in putting me out of my misery?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could use fopen() and fwrite() to write text to a new file. print_r() could be used to get the structure of the data or you could write the post var itself to the file. But since your client side code is not sending any POST data, use $_GET on the php side instead of $_POST. Here's an example:

$f = fopen("post_log.txt", 'w'); // use 'w' to create the file if not exists or truncate anew if it does exist. See php.net for fopen() on other flags.

fwrite($f, print_r($_GET, true)); // the true on print_r() tells it to return a string

// to write just the Aeggs value to the file, use this code instead of the above fwrite:

fwrite($f, $_GET["Aeggs"]);

fclose($f);

NOTE: The 2nd param to $.post() would contain the "post" data. Since you dont have that in your code, the $_POST on the PHP side will be an empty array.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!