2014-01-22 48 views
0

我有這個問題,我無法設法完成這個簡單的腳本。基本上,我想從表單中獲取一篇文章,將其分配給一個變量,然後將該變量追加到一個預先存在的example.txt文件中。我對缺乏示例表示歉意,在我和我的分社出去吃晚餐後,我轉到了手機。任何人都可以發佈一個簡單的例子,並讓我從這個基礎上建立起來?在此先感謝php後變量,然後附加到txt

+0

當你有機會回到桌面時,給我們一個當前代碼的例子。 – Coderchu

+0

在詢問你是否打算得到答案之前,請先閱讀*如何詢問*網頁。 –

回答

2

以下應該給你一個基礎開始。

<?php 

// Check to see if the form was submitted 

if(isset($_POST['action']) && $_POST['action'] == 'add'){ 

    $file = 'example.txt'; 

    // Open the file to get existing content 
    $current = file_get_contents($file); 

    // Append a new person to the file 
    $current .= $_POST['test']; 

    // Write the contents back to the file 
    file_put_contents($file, $current); 
} 

?> 

<form method="POST"> 

    <!-- This becomes PHP variable $_POST['test'] --> 
    <input type="text" name="test" /> 

    <input type="hidden" name="action" value="add" /> 
    <input type="submit" value="Add"/> 
</form> 

查看http://us3.php.net/file_put_contents瞭解更多信息。

+0

這種方式很有魅力,非常感謝你 – user3221563

-1

如果你的HTML表單有這樣一個文本字段 <input type="text" name="firstName" value="Name"> 你可以在你的PHP腳本使用 $_POST['firstName'];. 如果要附加在PHP中的文本訪問該值,你可以簡單地做它用「」 例如:$post_string = 'Ex:' . $post_string;

+0

哦!爲什麼倒票.. :( – IBunny

相關問題