user2850689
user2850689

Reputation: 11

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

I am trying to make a comment 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 comments or questions, or concerns.

I have researched many ways of how I could possibly make a comment box, how to write it out to a file, how to show the comments, 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 come up with something along the lines of this:

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

<div class="commentf">
    <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="comment">Text: </LABEL>
                              <TEXTAREA type="text" id="comment">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['comment'])))) {
        $data = $_POST['name'] . '-' . $_POST['email'] . '-' . $_POST['subject'] . '-' . $_POST['comment'] . "\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 comments.

    <div class="postcomment">
             <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['comment']; ?> <TEXTAREA type="text" id="comment"></TEXTAREA>
             </FORM>
        </div>

Upvotes: 1

Views: 22655

Answers (2)

jophab
jophab

Reputation: 5509

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="postcomment">
 <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['comment']; ?> <TEXTAREA type="text" name="comment"></TEXTAREA>
 </FORM>

Upvotes: 0

michael spencer
michael spencer

Reputation: 27

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

<?php
if ($_POST){

$name = $_POST['name'];
$content = $_POST['commentContent'];
$handle = fopen("comments.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="commentContent"></textarea></br>
Name: <input type = "text" name = "name"></br>
<input type = "submit" value = "post!"></br>
</form>

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

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

Upvotes: 1

Related Questions