1

私はウェブサイトでいくつかの機能を実行しようとしている初心者です。残念ながら権限がほとんどないので、データベースや jQuery、JavaScript を使用せずに、できるだけ簡単にコメント ボックスを作成したいと考えています。

私は多くの検索を行いましたが、最も簡単な方法は、PHP スクリプトが入力されたテキストを追加するコメント フォームを含むログのような HTML を作成することだと思います。これは私がこれまでに何とか作り上げたものです:

$file = "updates.html";
$fh = fopen($file, 'a');
$file = "updates.html";
$fh = fopen($file, 'a');
$comment = echo $_POST["update"] \n";
fwrite($fh, $comment);
fclose($fh);

updates.html ファイルには、アクションが上記の内容の php ファイルを指すコメント ボックスがあります。もちろん、機能しません。解析エラーがありますが、そこで変数を使用する方法がわかりません(それが問題の原因である場合)。私はそれを行う方法を理解することはできません.何か提案はありますか? ありがとう!

4

5 に答える 5

2

あなたが何をしたいのかわからない....

 <?php
  if(isset($_POST['update'])) {
     // if a request update exists
     $file = "updates.html";
     file_put_contents($file, $_POST['update']."\n",FILE_APPEND);
  }
  ?>
于 2013-06-11T11:42:10.483 に答える
2

ファイルを 2 回開いた。$_POST['update] をエコーする必要はありません

<?php 
$file = "updates.html"; 
$fh = fopen($file, 'a'); 
$comment = $_POST["update"] . "\n"; 
fwrite($fh, $comment); 
fclose($fh); 
?>
于 2013-06-11T11:41:30.990 に答える
1

この回答は、将来同様の質問をする可能性がある人のためのものです。

データベースなしでコメントを追加することは非現実的ですが、実行可能です。以下は、あなたがそれについて行く方法です。

ステップ 1: ファイルを作成し、comment.php のように .php 拡張子を付けて保存します ステップ 2. 通常の html フォームを作成し、フォーム メソッド = "post" とフォーム アクションをファイルの名前に設定します。 php」

<h3> Add a comment here </h3>

<form action="comment.php" method="post">
<label for="name">Name:</label>
<input type="text" name="yourname"><br>
<label for="name">Comment:</label> 
<textarea name="comment" id="comment" cols="30" rows="10"></textarea>
<input type="submit" value="submit">
</form>

` ステップ 3. 同じファイル comment.php 内に php スクリプトを記述して、フォームからのデータを処理します。スクリプトをphpタグで囲むことを忘れないでください

<?php

$yourname = $_POST['yourname'];
$comment = $_POST['comment'];

// format the comment data into how you want it to be displayed on the page
$data = $yourname . "<br>" . $comment . "<br><br>";

//Open a text file for writing and save it in a variable of your chosen.    
//Remember to use "a" not "w" to indicate write. Using 'w' will overwrite 
// any existing item in the file whenever a new item is written to it.

$myfile = fopen("comment.txt", "a"); 

//write the formatted data into the opened file and close it
fwrite($myfile, $data); 
fclose($myfile);

// Reopen the file for reading, echo the content and close the file
$myfile = fopen("comment.txt", "r");
echo fread($myfile,filesize("comment.txt")); 

?>              
于 2016-08-16T01:53:24.833 に答える
0

ある種の区切り文字が必要なため、ファイルに入れると問題が発生する可能性があります.base64でエンコードし、エントリの最後に \n を追加できます

 $input = base64_encode(htmlspecialchars($_POST['update'])); //consider using strip_tags as well to avoid injections

 file_put_contents("updates.html", $input."\n");

エントリを取得するには

 $entires = file("updates.html");
 if(count($entries) > 0)
 {
  foreach($entries as $entry)
  {
   echo base64_decode($entry);
  }
 }
 else
 {
   echo 'no entries so far';
 }

Db を使用したくない場合は、少なくともSimpleXmlの使用を検討する必要があります。

于 2013-06-11T11:46:05.293 に答える