简体   繁体   English

JS $.post > dump PHP $_POST 数据到文件

[英]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.我从一个button onclick=调用了一些 JS,它发布到 PHP 文件。 I'd like to take this POST data and just write it to a file, nothing fancy, just dumping it raw.我想获取此 POST 数据并将其写入文件,没什么特别的,只是将其原始转储。

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.我尝试了各种方法,但似乎都不起作用——要么根本不写入数据,要么写入“()”、“[]”(如果尝试将 POST 数据编码为 JSON),或者只写入“数组”一词“ 等等。

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.我试过将上面的内容存储为 $data 并写入它。 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").我主要尝试使用 fopen/write/close 来做这件事(因为那是我真正“知道”的全部)。 File is writable.文件可写。

(part of the) JS I'm using to POST: (部分)我用来发布的 JS:

(from button onclick="send('breakfast') ) (来自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.我不想从 POST 数据中提取(?)值,只是将其“按原样”写入文件,而且我不关心格式等。

Would someone please assist in putting me out of my misery?有人可以帮助我摆脱痛苦吗?

You could use fopen() and fwrite() to write text to a new file.您可以使用fopen()fwrite()将文本写入新文件。 print_r() could be used to get the structure of the data or you could write the post var itself to the file. print_r()可用于获取数据的结构,或者您可以将 post var 本身写入文件。 But since your client side code is not sending any POST data, use $_GET on the php side instead of $_POST .但是由于您的客户端代码没有发送任何 POST 数据,因此在 php 端使用$_GET而不是$_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.注意: $.post()的第二个参数将包含“post”数据。 Since you dont have that in your code, the $_POST on the PHP side will be an empty array.由于您的代码中没有它,因此 PHP 端的$_POST将是一个空数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM