Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

344
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda