我如何在不替换旧文本的情况下向文本区域添加文本

问题描述 投票:0回答:1

我想将文本添加到文本区域。但是不要更换文本应如下所示

你好当我单击按钮->约翰你好

即是我的代码。当我写东西并想将其添加到文本区域时,它会取代它但我希望它增加了它。像这样:(https://www.mediacollege.com/internet/javascript/form/add-text.html)|但是用PHP和喜欢回声或打印我是怎么做到的。

<?php
    $commentFile = fopen("comments.txt", "a");
    $comments = $_POST['comments'].PHP_EOL;

    fwrite($commentFile, $comments);
    fclose($myfile);
?>
<h5>     
    <textarea readonly id="comment" style="font-family: sans-serif; color: black" >                           
        <?php echo "$comments"; ?>
    </textarea>
</h5>
<div>         
    <form action="" method="post">
        <label> Write a comment</label>
        <textarea id="writec" name="comments" input type="text"></textarea>
        <input type="submit" name="button" value="Submit"/>
    </form>
</div>
php
1个回答
0
投票

尝试使用Java编写此代码

 <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
   <?php
    $commentFile = fopen("comments.txt", "a");
    $comments = $_POST['comments'].PHP_EOL;

    fwrite($commentFile, $comments);
    fclose($myfile);
?>
<h5>     
    <input readonly id="comment" style="font-family: sans-serif; color: black" value="<?php echo "$comments"; ?>">                           


</h5>
<div>         
    <form action="#">
        <label> Write a comment</label>
        <input id="writec" name="comments" type="text">
        <button type="submit" class='btn btn-secondary' name="button" onClick="addtext();">
    </form>
</div>
<script type="text/javascript">
function addtext() {

    var txt = (document.getElementById("comment").value)+(document.getElementById("writec").value);
    //alert(txt);
    document.getElementById("comment").value=txt;
}
    </script>
© www.soinside.com 2019 - 2024. All rights reserved.