我想要一个页面,我可以在其中输入文本到输入表单,然后将该文本插入到文本文件中的适当位置。
例如,输入表单将包含2个输入表单字段: 1.多少比萨饼? 哪个城市?
它将更新一个文字文件:
(2) 中有(1)比萨饼。
纽约有5个披萨
很简单,但我该怎么做?
答案 0 :(得分:0)
PHP:
<?php
file_put_contents('file.txt', 'There are ' . $_POST['pizzas'] . ' pizzas in ' . $_POST['city']);
?>
HTML:
<form action="script.php" method="POST">
Pizzas: <input type="text" name="pizzas"><br />
City: <input type="text" name="city"><br />
<input type="submit" value="Save">
</form>
答案 1 :(得分:0)
使用JSON。它可以让你轻松编辑数据(因为它很酷)。
if ( empty( $_POST['pizzaCount'] ) === false && empty( $_POST['city'] ) === false )
{
$savePath = 'folder/folder2/save.txt';
$content = array();
//does the file already exist?
if ( ( $fileContents = file_get_contents( $savePath ) ) !== false )
{
//get the old data
$content = json_decode( $fileContents , true );
}
//add the new data
$content[] = array( 'pizzaCount' => $_POST['pizzaCount'] , 'city' => $_POST['city'] );
//decode the new data
$content = json_encode( $content );
file_put_contents( $savePath , $content );
}