JS $.post > 将 PHP $_POST 数据转储到文件



我已经尝试了几个小时,终于放弃了。正如你所知,我有一个巨大的角落,几乎不知道自己在做什么。。。

我有一些JS是从一个button onclick=调用的,它将POST到一个PHP文件中。我想把这个POST数据写到一个文件中,没有什么特别的,只是把它原始转储。

我尝试了各种方法,但似乎都不起作用——要么根本不写数据,要么写";(("[]";(如果试图将POST数据编码为JSON(,或者仅仅是单词"JSON";阵列";等等

我尝试过的方法;

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

我已经尝试过将上面的内容存储为$data并编写它。我所做的一切似乎都不起作用。

我主要是想用fopen/write/close来做这件事(因为这是我真正"知道"的全部(。文件是可写的。

(我用来POST的JS的一部分(:

(来自button onclick="send('breakfast')(

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

我不想从POST数据中提取(?(值,只想写它";"照原样";到一个文件,我不介意格式化等

有人能帮助我摆脱痛苦吗?

您可以使用fopen()fwrite()向新文件写入文本。print_r()可以用于获取数据的结构,也可以将post-var本身写入文件。但是,由于客户端代码没有发送任何POST数据,因此在php端使用$_GET而不是$_POST。这里有一个例子:

$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);

注意:$.post()的第二个参数将包含";张贴";数据由于您的代码中没有,PHP端的$_POST将是一个空数组。

最新更新