簡體   English   中英

從表單獲取用戶輸入,使用php寫入文本文件

[英]Get user input from form, write to text file using php

作為訂閱者獲取的一部分,我希望從html表單中獲取用戶輸入的數據,並使用php將其寫入制表符分隔的文本文件。所寫的數據需要通過制表符分隔並附加在其他數據下方。

單擊表單上的訂閱后,我希望它刪除表單並在div中顯示“感謝訂閱”等小消息。

這將在wordpress博客上,並包含在彈出窗口中。

以下是具體細節。 任何幫助深表感謝。

變量/輸入是

$Fname = $_POST["Fname"];
$email = $_POST["emailPopin"];
$leader = $_POST["radiobuttonTeamLeader"];
$industry = $_POST["industry"];
$country = $_POST["country"];
$zip = $_POST["zip"];

$ leader是一個雙選項單選按鈕,其中“是”和“否”為值。

$ country是一個下降的40個左右的國家。

所有其他值都是文本輸入。

除了操作之外,我已經完成了所有基本的表單代碼並准備就緒,我真正需要知道的是:

如何使用php寫入制表符分隔的文本文件,並在提交感謝信息后換出表單?

再次感謝所有的幫助。

// the name of the file you're writing to
$myFile = "data.txt";

// opens the file for appending (file must already exist)
$fh = fopen($myFile, 'a');

// Makes a CSV list of your post data
$comma_delmited_list = implode(",", $_POST) . "\n";

// Write to the file
fwrite($fh, $comma_delmited_list);

// You're done
fclose($fh);

在impode中用\\ t替換標簽

以追加模式打開文件

$fp = fopen('./myfile.dat', "a+");

並將所有數據放在那里,標簽分開。 最后使用新行。

fwrite($fp, $variable1."\t".$variable2."\t".$variable3."\r\n");

關閉你的文件

fclose($fp);
// format the data
$data = $Fname . "\t" . $email . "\t" . $leader ."\t" . $industry . "\t" . $country . "\t" . $zip;

// write the data to the file
file_put_contents('/path/to/your/file.txt', $data, FILE_APPEND);

// send the user to the new page
header("Location: http://path/to/your/thankyou/page.html");
exit();

通過使用header()函數重定向瀏覽器,可以避免用戶重新加載頁面並重新提交數據時出現問題。

這是fwrite()最好的例子,你最多只能使用3個參數,但是附加一個“。” 您可以根據需要使用盡可能多的變量。

if isset($_POST['submit']){
    $Fname = $_POST["Fname"];
    $email = $_POST["emailPopin"];
    $leader = $_POST["radiobuttonTeamLeader"];
    $industry = $_POST["industry"];
    $country = $_POST["country"];
    $zip = $_POST["zip"];

    $openFile = fopen("myfile.ext",'a');
        $data = "\t"."{$Fname}";
        $data .= "\t"."{$email}";
        $data .= "\t"."{$leader}";
        $data .= "\t"."{$industry}";
        $data .= "\t"."{$country}";
        $data .= "\t"."{$zip}";

    fwrite($openFile,$data);
    fclose($openFile);
}

非常非常簡單:表單數據將被收集並存儲在$ var中,$ var將被寫入filename.txt \\ n將添加一個新行。 文件追加禁止覆蓋文件

<?php
$var = $_POST['fieldname'];
file_put_contents("filename.txt", $var . "\n", FILE_APPEND);
exit();
?>

換出表單相對容易。 確保將表單的操作設置為同一頁面。 只需將表單包裝在“if(!isset($ _ POST ['Fname']))”條件中。 在將表單發布到“else {}”部分之后,放置要顯示的任何內容。 因此,如果表單已過帳,則會顯示“else”子句中的內容; 如果表單未發布,將顯示“if(!isset($ _ POST ['Fname']))”的內容,即表單本身。 您不需要其他文件來使其工作。
要在文本文件中寫入POSTed值,只需按照上面提到的其他方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM