2

データベースサーバーを使用せずに、txtファイルまたは受動的にクエリできるその他のものを使用してコメントボックスを作成する必要があります。私はPHPプログラミングにかなり慣れていないので、最初のアイデアはテキストファイルを使用することでした。私が論理的に考えることができる限り、これを達成するための一般的なコードは次のようになります:

    <html>
<head></head>
<body>
<form method = "post">
<textarea name = "txt" cols = "25" rows = "5">
Place your comment here ...
</textarea><br><br>
<input type = "submit" value = "Submit" onclick = "<?php
    $com = $_POST["txt"];
    $file = fopen("inrg.txt", "a");
    fwrite($file, "<br>");
    for($i=0; $i <= strlen($com) - 1; $i++)
        {
        fwrite($file, $com[$i]);
        if($i % 37 == 0 && $i != 0)
            fwrite($file, "<br/>");
        }
    fwrite($file, "<br>------------------------------------------");
    fclose($file);
?>">
<br>
</form>
<font face = "Times New Roman"><b><p>Textul introdus este: </p></b></font>
<font face = "Comic Sans MS" color = "red" size = "2" >
<?php
$file = fopen("inrg.txt", "r");
echo fread($file, filesize("inrg.txt"));
fclose($file);
?>
</font>
</body>
</html>

まだ派手なものはなく、審美的な面でいくつかの改善が必要です。コメントボックスに何かを送信すると、正しく表示されますが、Webブラウザでリロードすると、最後に投稿されたコメントがページをリロードした回数だけ再度投稿されます。また、PHPを使用して、最初の「コメントをここに配置...」を非表示にする方法がある場合

4

4 に答える 4

1
    <!doctype html>
    <html>
      <head>
        <meta charset="UTF-8">
        <title>Example document</title>
      </head>
      <body>
<?php
// the relative path to the file
$fname = "theFile.txt";
// read in the file if present
if(file_exists($fname)) $txt = file_get_contents($fname);

// if the user pushes the submit button
if(isset($_POST["txt"])){
    $txt = $_POST["txt"];   // get the entered content
    file_put_contents($fname,$txt);    // write the content to the file
}
?>
<form method="post" action="#">
<textarea name = "txt" cols = "25" rows = "5">
<?php echo $txt; ?>
</textarea><br />
<input type="submit" name="submit" value="submit" />
</form>
      </body>
    </html>

スクリプトが存在するフォルダーは、PHP(= WebServer)で書き込み可能である必要があります。このスクリプトは重要であり、クロスサイドスクリプトハッキングに対するセキュリティはありません。改行に関する問題がある可能性があります。

于 2012-11-21T20:56:04.350 に答える
0

ページをそれ自体にリダイレクトする1つの方法は次のとおりです。

<html>
<head></head>
<body>
<form method="post">
  <textarea name="txt" cols="25" rows="5"></textarea>
  <br><br> <input type="submit" value="Submit" name="submit" />

  <?php
  if ( isset( $_POST[ 'submit' ] ) ) {
    $com  = $_POST[ "txt" ];
    $file = fopen( "inrg.txt", "a" );
    fwrite( $file, "<br>" );
    for ( $i = 0; $i <= strlen( $com ) - 1; $i++ ) {
      fwrite( $file, $com[ $i ] );
      if ( $i % 37 == 0 && $i != 0 ) fwrite( $file, "<br/>" );
    }
    fwrite( $file, "<br>------------------------------------------" );
    fclose( $file );
   echo '<script type="text/javascript">window.location ="";</script>'; // Add here
  }
  ?>

  <br>
</form>
<font face="Times New Roman"><b><p>Textul introdus este: </p></b></font>
<font face="Comic Sans MS" color="red" size="2">
  <?php
  if (file_exists("inrg.txt")) {
  $file = fopen( "inrg.txt", "r" );
  echo fread( $file, filesize( "inrg.txt" ) );
  fclose( $file );
  }
  ?>
</font>
</body>
</html>
于 2012-11-21T20:42:58.787 に答える
0

これを試してみてください:

anotherpage.php

<?php
        if($_POST){ // is a message has been posted

            $fp = fopen('inrg.txt', 'a'); // open the text file and append using the 'a'
            fwrite($fp, $_POST['txt']."\n");
            fclose($fp);
            $message_message_to_user = "Message added to file. Wait a sec and you will be redirected.";
        }

            header("Location: theformpage.php"); // regardless of if there is a post - send the user back to the original page
            exit();
?>

theformpage.php

<html>
<head>
</head>
<body>
<form method="post" action="anotherpage.php">
    <textarea name = "txt" cols = "25" rows = "5">Place your comment here ...</textarea><br><br>
    <input type = "submit" value="Submit">
    <br>
</form>
<hr />
<?php
    // this reads all of the lines of the file and spits them out on the screen
    $fh = fopen('inrg.txt','r');
    while ($line = fgets($fh)){
        echo nl2br($line); // nl2br() converts new lines to <br>'s
    }
    fclose($fh);

?>
</body>
</html>
于 2012-11-21T20:49:59.753 に答える
0

パーツごとに行きましょう:

1)リロード時にフォームを再送信しているため、コメントは何度も投稿されます。これを回避するには、投稿後にページをリダイレクトする必要があります。この手法はと呼ばれRedirect after Postます。あなたはそれを検索する必要があります、それは面白いです。

2)ページの読み込みが完了すると、PHPは実行を終了するため、PHPにはテキストを非表示にする方法がありません。そのためには、Javascriptを学ぶ必要があります。私もJavaScriptを学んでいて、難しいと思いましたが、Simply Javascriptの本を読んで、JSを非常にうまく紹介してくれます。

ご存知のとおり、コメントボックスで「onClick」イベントをリッスンしてから、コメントボックスをクリアするJS関数を呼び出す必要があります。

次のようになります:

... id="thisBoxId" onclick="cleartext('thisBoxID')" >Enter a comment here...<...

とJS:

function cleartext( myitem ) {
   myitem.text.value = "";
}

もちろん、一度実行すると、ページをリロードするまでテキストが戻ってくることはありません。戻ってくるようにするには、onLostFocusイベントを使用してテキストを補充する必要があります。

于 2012-11-21T20:50:13.380 に答える