输入按钮到textarea和writingorder

时间:2013-04-26 14:27:44

标签: php javascript textarea

我很难做一个简单的聊天功能。 我的代码有两个问题:

  1. 我希望新帖子显示在文本文件的顶部。
  2. 我希望输入按钮能够将一些html代码写入textarea,以便在使用时更容易合成文本。
  3. 以下是代码:

    <script type="text/javascript">
    forma1 = "<B>   </B>"
    forma2 = "<font color="#B2E0B2">        </font>"
    forma3 = "<font color="#C2E0FF">        </font>"
    forma4 = "<font color="#FFC2C2">        </font>"
    </script>
    
        <form method="post">
    <div align="center">
      <input name="navn" type="text" size="30" style="color:#68748D;" value="Navn" onfocus="if (this.value == 'Navn') this.value = '';"/>
        <input type="button" onkeypress="form1('area');"/>
        <input type="button" onkeypress="form2('area');"/>
        <input type="button" onkeypress="form3('area');"/>
        <input type="button" onkeypress="form4('area');"/>
      <br>
      <textarea name="txt" cols="55" rows="5" id="area"></textarea>
      <br><br> <input type="submit" value="Send" name="submit" class="a" />
    </div>
      <?php
      if ( isset( $_POST[ 'submit' ] ) ) {
        $dude  = $_POST ['navn'];
        $time = gmdate("M d Y H:i:s",time()+(2*60*60));
        $com  = $_POST['txt'];
        $fp = $file = fopen( "msgs.txt", "a");
        fwrite($file, $time);
        fwrite($file, "<br>");
        fwrite($file, '<font color="#68748D" style="font-weight:800;" size="+2">');
        fwrite($file, $dude);
        fwrite($file, '</font>');
        fwrite($file, "<br>");
        fwrite($file, $com);
        fwrite($file, "<br>________________________________________________________________________<br>" );
        fclose($fp);
       echo '<script type="text/javascript">window.location ="";</script>';
      }
      ?>
    
      <br>
    </form>
    <font><b><p>Output: </p></b></font>
    <fontcolor="#000" size="2">
      <?php
      if (file_exists("msgs.txt")) {
      $file = fopen( "msgs.txt", "r" );
      echo fread( $file, filesize( "msgs.txt" ) );
      fclose( $file );
      }
      ?>
    </font>
    

    谢谢。

1 个答案:

答案 0 :(得分:0)

我可以使用Need to write at beginning of file with PHP回复写入文件开头的内容:

$file_data = "Stuff you want to add\n";
$file_data .= file_get_contents('msgs.txt');
fwrite('msgs.txt', $file_data);

php手册说fwrite和file_put_contents是相同的,所以这应该仍然有用