我已經嘗試了幾個小時,最后放棄了。正如你所知道的,我有一個巨大的菜鳥,幾乎不知道我在做什么......
我有一些 JS 從button onclick=哪個 POST 呼叫到 PHP 檔案。我想把這個 POST 資料寫到一個檔案中,沒什么特別的,只是把它原始地轉儲。
我嘗試了各種方法,但似乎都不起作用 - 要么根本不寫資料,要么寫“()”,“[]”(如果試圖將 POST 資料編碼為 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 資料中提取(?)值,只是將其“按原樣”寫入檔案,并且我不關心格式化等。
有人可以幫助我擺脫痛苦嗎?
uj5u.com熱心網友回復:
您可以使用fopen()andfwrite()將文本寫入新檔案。print_r()可用于獲取資料的結構,或者您可以將 post var 本身寫入檔案。但由于您的客戶端代碼沒有發送任何 POST 資料,$_GET請在 php 端使用而不是$_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()將包含“發布”資料。由于您的代碼中沒有它,$_POST因此 PHP 端的 將是一個空陣列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/434144.html
標籤:javascript php
上一篇:Cookie值重復
下一篇:對Reactnative的承諾
