Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Guestbook repost when refresh

    The problem I am having is when I post to my guestbook I get what I put in but if i refresh the page it sends the information again. So what I want to figure out is how to prevent this. Here is a example of my code


    <?php


    $name = $_GET["txt_name"];
    $len = strlen($name);
    if($len > 0)
    {
    $email = $_GET["txt_email"];
    $comment = $_GET["txt_comment"];
    $date = time();

    $query = "INSERT INTO guestbook (autoID, name, email, comment, date_auto)
    VALUES (NULL, '$name', '$email', '$comment', '$date')";

    mysql_query($query, $connection) or die (mysql_error());
    }

    ?>
    <html>
    <head>
    <title>Welcome to Keiths Guestbook</title>
    </head>
    <body>
    <center>
    <form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="GET">
    <font face="arial" size="1">
    Name: <input type="text" name="txt_name">&nbsp;
    Email: <input type="text" name="txt_email"><br><br>
    Comment: <br>
    <textarea style="width: 75%" rows="10" name="txt_comment"></textarea>
    <center><input type="submit" value="Submit"></center>
    </font>

    <table bgcolor="#AAAAAA" border="0" width="75%" cellapceing="1" cellpading="2">
    <?php
    $query = "Select * FROM guestbook ORDER BY date_auto";
    $result = mysql_query($query, $connection);

    for ($i = 0; $i < mysql_num_rows($result); $i++)
    {
    $name = mysql_result($result, $i, "name");
    $email = mysql_result($result, $i, "email");
    $email_len = strlen($email);
    $comment = mysql_result($result, $i, "comment");
    $comment = nl2br($comment);
    $date = mysql_result($result, $i, "date_auto");
    $show_date = date("H:i:s m/d/Y", $date);

    if ($i % 2)
    {
    $bg_color="#EEEEEE";
    }
    else
    {
    $bg_color="#E0E0E0";
    }

    echo '
    <tr>
    <td width="100%" bgcolor="'.$bg_color.'">
    <font face="arial" size="2">';
    if ($email_len > 0)
    {
    echo '<b>Name:</b> <a href="mailto:'.$email.'">'.$name.'</a>';
    }
    else
    {
    echo '<b>Name:</b>'.$name;
    }
    echo '
    <br>
    <b>Comment:</b> '.$comment.'
    </font>
    <td>
    <td width="1%" valign="top" nowrap bgcolor="'.$bg_color.'">
    <font face="arial" size="2">
    <b>Date:</b> '.$show_date.'
    </font>
    </td>
    </tr>
    ';
    }
    ?>

    </table>

    </center
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts
    i am a newbie, this is my try
    Code:
    if (!isset($registered)) {
    setcookie('registered','true',time()+300);//limit 5 mininutes
    $query = "INSERT INTO guestbook (autoID, name, email, comment, date_auto) 
    VALUES (NULL, '$name', '$email', '$comment', '$date')";
    } else {
    echo "registered, dont submit twice!";
    }
    and, i suggest you do some validation to kick out html codes and special chars.
    flying dagger

  • #3
    4xz
    4xz is offline
    Regular Coder
    Join Date
    Aug 2004
    Location
    localhost
    Posts
    163
    Thanks
    0
    Thanked 1 Time in 1 Post
    Change the top part to :

    Code:
    <?php
    
      
      $name = $_GET["txt_name"];
      $len = strlen($name);
      if($len > 0)
      {
         $email   = $_GET["txt_email"];
         $comment = $_GET["txt_comment"];
         $date    = time();
         
         $query   = "INSERT INTO guestbook (autoID, name, email, comment, date_auto) 
                     VALUES (NULL, '$name', '$email', '$comment', '$date')";
         $result= mysql_query($query, $connection);            
         if($result) {
             header("Location: guestbook.php?alert=Entry%20added%20to%20guestbook");
         } else {
            die (mysql_error());
    
      }
    
    ?>
    ....
    and put this somewhere in the BODY of the page :

    Code:
    <?php
    if(isset($_GET[alert]) {
       echo "<h1>".$_GET[alert]."</h1>";
    }
    ?>
    The trick is to redirect the user that has just posted the entry to a 'fresh' guestbook, without the request parameters (in this case $_GET["txt_name"]).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •