最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I make a comment box, using preferablely javascript or php, and then show the comments? - Stack Overflow

programmeradmin2浏览0评论

I am trying to make a ment box for my website. I can make the background and other items using HTML and CSS, but I want people to be able to leave ments or questions, or concerns.

I have researched many ways of how I could possibly make a ment box, how to write it out to a file, how to show the ments, and how to update the file, but because I personally don't know PHP or JavaScript I don't know how to do any of that. I have looked at other peoples coding and have managed to e up with something along the lines of this:

This is for the form, it's an HTML:

<div class="mentf">
    <table>
        <tbody>
             <FORM action="submit.html" method="post">
                <tr>
                    <td><LABEL for="name">Name: </LABEL>
                              <INPUT type="text" id="name"></td>
                </tr>
                <tr>
                    <td><LABEL for="email">E-Mail: </LABEL>
                               <INPUT type="text" id="email"></td>
                </tr>
                <tr>
                    <td><LABEL for="subject">Subject: </LABEL>
                              <INPUT type="text" id="subject"></td>
                </tr>
                <tr>
                    <td><LABEL for="ment">Text: </LABEL>
                              <TEXTAREA type="text" id="ment">Comment:</TEXTAREA></td>
                </tr>
                <tr>
                    <td><INPUT type="submit" value="Submit"> <INPUT type="reset"></td>
                </tr>
            </FORM>
        </tbody>
    </table>
</div>

I am trying to make a ment box for my website. I can make the background and other items using HTML and CSS, but I want people to be able to leave ments or questions, or concerns.

I have researched many ways of how I could possibly make a ment box, how to write it out to a file, how to show the ments, and how to update the file, but because I personally don't know PHP or JavaScript I don't know how to do any of that. I have looked at other peoples coding and have managed to e up with something along the lines of this:

This is for the form, it's an HTML:

<div class="mentf">
    <table>
        <tbody>
             <FORM action="submit.html" method="post">
                <tr>
                    <td><LABEL for="name">Name: </LABEL>
                              <INPUT type="text" id="name"></td>
                </tr>
                <tr>
                    <td><LABEL for="email">E-Mail: </LABEL>
                               <INPUT type="text" id="email"></td>
                </tr>
                <tr>
                    <td><LABEL for="subject">Subject: </LABEL>
                              <INPUT type="text" id="subject"></td>
                </tr>
                <tr>
                    <td><LABEL for="ment">Text: </LABEL>
                              <TEXTAREA type="text" id="ment">Comment:</TEXTAREA></td>
                </tr>
                <tr>
                    <td><INPUT type="submit" value="Submit"> <INPUT type="reset"></td>
                </tr>
            </FORM>
        </tbody>
    </table>
</div>

And this is the PHP file (saved as an HTML, for some reason when I try to open it as a PHP file it opens a save as box instead of running the PHP, so I just saved it as a HTML) that "processes" the information:

<?php
        if(isset($_POST['name']) && isset($_POST['email'] && isset ($_POST['subject'] && isset ($_POST['ment'])))) {
        $data = $_POST['name'] . '-' . $_POST['email'] . '-' . $_POST['subject'] . '-' . $_POST['ment'] . "\n";
        $ret = file_put_contents('HAS.txt', $data, FILE_APPEND | LOCK_EX);
        if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}

Lastly, this is part of the html that I first displayed, so that it would show the ments.

    <div class="postment">
             <FORM>
                    <br>Name:</b> <?php echo $_POST['name']; ?> <INPUT type="text" id="name">
                    <br>E-Mail:</b> <?php echo $_POST['email']; ?> <INPUT type="text" id="email">
                    <br>Subject:</b> <?php echo $_POST['subject']; ?> <INPUT type="text" id="subject">
                    <br>Comment:</b> <?php echo $_POST['ment']; ?> <TEXTAREA type="text" id="ment"></TEXTAREA>
             </FORM>
        </div>

Share Improve this question edited Sep 23, 2020 at 14:38 Adriaan 18.2k7 gold badges44 silver badges86 bronze badges asked Oct 6, 2013 at 0:04 user2850689user2850689 111 gold badge1 silver badge2 bronze badges 8
  • Does the last script provided succeed or what happends when you submit the form? – Serge Seredenko Commented Oct 6, 2013 at 1:59
  • Your <FORM> is horribly violating your <table> integrity. Run this through a validator. Please. – Mr Lister Commented Oct 6, 2013 at 9:09
  • Really, all that happens is it takes me to the submit.html... and from their nothing... For the past few days I've been learning Java Scripting and have kind of came up with my own code... It works a little better, but no cigar... It at least will take me to another page and in the address box display the words that I typed into it... but the problem is ,as of right now, for me is that I can't make the text display onto the page... then after that I want to change the code to write it to a text document or something and save it so I can write it out to the webpage... so I'll send you the code. – user2850689 Commented Oct 7, 2013 at 21:30
  • dl.dropboxusercontent./u/101704699/send1.html and dl.dropboxusercontent./u/101704699/send2.html... these are the links to access the files... for my sake... and your sake I removed any unnecessary tags (i.e. div's, p's, span's, etc.) thanks for the replies! – user2850689 Commented Oct 7, 2013 at 21:42
  • I mean jQuery... not java scripting... all though... their might be a little of that in their to... – user2850689 Commented Oct 7, 2013 at 21:48
 |  Show 3 more ments

2 Answers 2

Reset to default 1

If you haven't found an answer yet heres a simple html way of creating ments section for your website it contains the php

<?php
if ($_POST){

$name = $_POST['name'];
$content = $_POST['mentContent'];
$handle = fopen("ments.html","a");
fwrite ($handle,"<b>" . $name . "</b></br>" . $content . "</br>");
fclose ($handle);}

?>

<html>
<body>

<form action="" method="POST">
Content: <textarea rows ="10" cols ="30" name="mentContent"></textarea></br>
Name: <input type = "text" name = "name"></br>
<input type = "submit" value = "post!"></br>
</form>

<?php include "ments.html"; ?>
</body>
</html>

just create a blank html called ments.html in same folder hope some help if no answer yet

Currently you are using the id of the input, textarea tags to access it values from $_POST. That is not possible. You have to use the name attribute of the tag to access it value from $_POST.

<div class="postment">
 <FORM>
        <br>Name:</b> <?php echo $_POST['name']; ?> <INPUT type="text" name="name">
        <br>E-Mail:</b> <?php echo $_POST['email']; ?> <INPUT type="text" name="email">
        <br>Subject:</b> <?php echo $_POST['subject']; ?> <INPUT type="text" name="subject">
        <br>Comment:</b> <?php echo $_POST['ment']; ?> <TEXTAREA type="text" name="ment"></TEXTAREA>
 </FORM>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论