0

入力フォームにテキストを入力し、そのテキストをテキスト ファイルの適切な場所に挿入できるページが必要です。

たとえば、入力フォームには 2 つの入力フォーム フィールドがあります。 1. ピザは何枚? 2.どの都市ですか?

次のようなテキスト ファイルが更新されます。

(2)に(1)個 のピザがあります。

ニューヨークには5つのピザがあります

とても簡単ですが、どうすればいいですか?

4

2 に答える 2

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>
于 2013-01-02T01:32:23.210 に答える
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 );

}
于 2013-01-02T01:37:22.720 に答える