theHaze Maze
theHaze Maze

Reputation: 19

Multiple feedback posting?

I made this form for people to leave feedbacks, but when you leave a feedback and refresh/reload the page, it posts it again, and again. Is there any way to make it say something like "The session expired"? Here is the code:

<form action="feedback.php" method="POST">
<table width="450px">
</tr>
<tr>
 <td valign="top">
  <label for="name">Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="name" maxlength="50" size="30">
 </td>
</tr>

<tr>
 <td valign="top">
  <label for="email">Email Address *</label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>

</tr>
<tr>
 <td valign="top">
  <label for="comment">Message *</label>
 </td>
 <td valign="top">
  <textarea  name="comment" maxlength="1000" cols="25" rows="6"></textarea>
 </td>

</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" name="add" value="Add FeedBack"> 
 </td>
</tr>
</table>
</form>

<?php
    if(isset($_POST['add'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $comment = $_POST['comment'];
        if($name){
            if($email){
                if($comment){
                    mysql_query("INSERT INTO comments (id, name, email, comment) VALUES ('','$name','$email','$comment')");
                }
                else
                    echo "You haven't entered any comment!";
            }
            else
                echo "You haven't entered an email address!";
        }
        else
            echo "You haven't entered your name!";
    }
?>

<?php
    $run = mysql_query("SELECT * FROM comments ORDER BY id DESC");
    $numrows = mysql_num_rows($run);
    if($numrows > 0){
        while($row = mysql_fetch_assoc($run)){
            $dbname = $row['name'];
            $dbcomment = $row['comment'];
            echo "Commented By $dbname<br>$dbcomment<br><br>";
        }
    }
    else
        echo "<br>There are no feedbacks made";
?>


Upvotes: 0

Views: 61

Answers (2)

Verhaeren
Verhaeren

Reputation: 1661

You can do this based on halfer approach:

At the top of your page:

<?php
    if(isset($_POST['add'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $comment = $_POST['comment'];
        if($name){
            if($email){
                if($comment){
                    mysql_query("INSERT INTO comments (id, name, email, comment) VALUES ('','$name','$email','$comment')");
                    //redirect
                    header("Location: mypage.php");
                }
                else
                    $msg = "You haven't entered any comment!";
            }
            else
                $msg = "You haven't entered an email address!";
        }
        else
            $msg = "You haven't entered your name!";
    }
?>

In your HTML code

<?php
    if(isset($msg)) {echo $msg;}
    $run = mysql_query("SELECT * FROM comments ORDER BY id DESC");
    $numrows = mysql_num_rows($run);
    if($numrows > 0){
        while($row = mysql_fetch_assoc($run)){
            $dbname = $row['name'];
            $dbcomment = $row['comment'];
            echo "Commented By $dbname<br>$dbcomment<br><br>";
        }
    }
    else
        echo "<br>There are no feedbacks made";
?>

Notice that you aren't redirecting if !$name or !$email or !$comment, which means: the browser will ask for form resubmition when refreshing the page, an event that I describe as ugly. You can redirect if any insertion is made too in order to avoid that:

<?php
    if(isset($_POST['add'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $comment = $_POST['comment'];
        if($name){
            if($email){
                if($comment){
                    mysql_query("INSERT INTO comments (id, name, email, comment) VALUES ('','$name','$email','$comment')");
                    //redirect
                    header("Location: mypage.php");
                }
                else
                    $msg = "You haven't entered any comment!";
            }
            else
                $msg = "You haven't entered an email address!";
        }
        else
            $msg = "You haven't entered your name!";

        if(isset($msg)) {header("Location: mypage.php?msg=".$msg);}
    }
?>

And then:

<?php
    if(isset($_GET['msg'])) {echo $_GET['msg'];}
    $run = mysql_query("SELECT * FROM comments ORDER BY id DESC");
    $numrows = mysql_num_rows($run);
    if($numrows > 0){
        while($row = mysql_fetch_assoc($run)){
            $dbname = $row['name'];
            $dbcomment = $row['comment'];
            echo "Commented By $dbname<br>$dbcomment<br><br>";
        }
    }
    else
        echo "<br>There are no feedbacks made";
?>

Upvotes: 1

user4349681
user4349681

Reputation:

<?php
    if(isset($_POST['add'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $comment = $_POST['comment'];
        $sql = mysql_query("INSERT INTO comments (id, name, email, comment) VALUES ('','".$name."','".$email."','".$comment."')");

        if($sql == ''){
                    echo "You haven't entered any comment! or You haven't entered an email address! or You haven't entered your name!";
        }
    }

    $run = mysql_query("SELECT * FROM comments ORDER BY id DESC");
    $numrows = mysql_num_rows($run);
    if($numrows > 0){
        while($row = mysql_fetch_array($run)){
            $dbname = $row['name'];
            $dbcomment = $row['comment'];
            echo "Commented By ".$dbname."<br>".$dbcomment."<br><br>";
        }
    }
    else
        echo "<br>There are no feedbacks made";
?>

Upvotes: 1

Related Questions