refreshing after "post"

Discussion in 'PHP' started by mezner, Jun 17, 2010.

  1. #1
    I'm working on a very simple CMS system where the admin can enter data into the html input's and then the php turns that data from the database and displays a table. So far so good. It's a very simple scipt. On the post.php page, I have an if statement where it will display a message saying the data was correctly added.

    Now here is the problem. If the admin wants to enter another row in the database he can't click refresh on the browser or it will re-enter the same row. Is there a way that after, say 3 seconds, the php code will go back to the form? Here is the code for the index:

    <?php
    
        $mysqli = new mysqli("localhost", "root", "", "pennypicks");
    
        // Create the query
        $query = "SELECT sku, name, price FROM recent ORDER by name";
    
       // Send the query to MySQL
       $result = $mysqli->query($query, MYSQLI_STORE_RESULT);
    
       // Iterate through the result set
       while(list($sku, $name, $price) = $result->fetch_row())
           printf("(%s) %s: \$%s <br />", $sku, $name, $price);
    
    	?>
    PHP:
    and for the post.php page:

    <?php
    	  if (isset($_POST['sku'])) {
            mysql_connect("localhost", "root", "");
            mysql_select_db("pennypicks");
    
            $sku = addslashes($_POST['sku']);
            $name = addslashes($_POST['name']);
            $price = addslashes($_POST['price']);
        
            $result = mysql_query("INSERT INTO recent (sku, name, price) VALUES ('$sku', '$name', '$price');");
            if ($result) {
                echo "Thanks for posting - Update added to mainpage!";
                exit;
            } else {
                echo "There was an error adding your entry - please try again, filling in all fields.";
         	}
        }
    ?>
    PHP:
    Again, when I hit refresh once the message shows, it adds in the same row, but only shows the "Thanks for posting - Update added to mainpage!". When I hit refresh, I want it to display the form again. Help would be appreciated!
     
    mezner, Jun 17, 2010 IP
  2. Cozmic

    Cozmic Member

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #2
    If you do not want a delay:
    
    // Replace "index.php" with the page to redirect to.
    header("Location: index.php");
    
    PHP:
    If you do want a delay:
    
    echo "<script type='text/javascript'>
    function delay() {
    setTimeout(\"window.location = 'index.php'\", 1000);
    }
    document.onload = delay();
    </script>";
    
    PHP:
     
    Cozmic, Jun 17, 2010 IP
  3. mezner

    mezner Peon

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I don't think you understand. There is no delay. After you submit the data, it displays the message. When you try and refesh the page (to go back to the form to insert another row) it just adds a duplicate row to the database that you just typed in, instead of bringing you back to the form so the admin can then insert another row.
     
    mezner, Jun 17, 2010 IP
  4. Cozmic

    Cozmic Member

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #4
    try this:

    $ref = $_SERVER['HTTP_REFERER'];
    header("Location: ".$ref);
    exit;
    PHP:
    This will send them back to where they submitted the form from.
     
    Cozmic, Jun 17, 2010 IP
  5. mezner

    mezner Peon

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Cool. Where would I put that in my script? at the end?
     
    mezner, Jun 17, 2010 IP
  6. Cozmic

    Cozmic Member

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #6
    Right after all the processing from the submission is done. Where you would normally echo out the confirmation message.
     
    Cozmic, Jun 17, 2010 IP
  7. mezner

    mezner Peon

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    So something like this?
    <?php
          if (isset($_POST['sku'])) {
            mysql_connect("localhost", "root", "");
            mysql_select_db("pennypicks");
            $ref = $_SERVER['HTTP_REFERER']
    
            $sku = addslashes($_POST['sku']);
            $name = addslashes($_POST['name']);
            $price = addslashes($_POST['price']);
       
            $result = mysql_query("INSERT INTO recent (sku, name, price) VALUES ('$sku', '$name', '$price');");
            if ($result) {
                echo "; header("Location: ".$ref);
                exit;";
    
            } else {
                echo "There was an error adding your entry - please try again, filling in all fields.";
            }
        }
    ?>
    PHP:
     
    mezner, Jun 17, 2010 IP
  8. Cozmic

    Cozmic Member

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #8
    This:

    
    <?php
          if (isset($_POST['sku'])) {
            mysql_connect("localhost", "root", "");
            mysql_select_db("pennypicks");
            $ref = $_SERVER['HTTP_REFERER'];
    
            $sku = addslashes($_POST['sku']);
            $name = addslashes($_POST['name']);
            $price = addslashes($_POST['price']);
       
            $result = mysql_query("INSERT INTO recent (sku, name, price) VALUES ('$sku', '$name', '$price');");
            if ($result) {
                header("Location: ".$ref);
                exit;
    
            } else {
                echo "There was an error adding your entry - please try again, filling in all fields.";
            }
        }
    ?>
    PHP:
     
    Cozmic, Jun 17, 2010 IP
  9. mezner

    mezner Peon

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Not working, it's doing exactly what it was before and displays this:


    Warning: Cannot modify header information
     
    mezner, Jun 17, 2010 IP
  10. Cozmic

    Cozmic Member

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #10
    That's because you used echo() before header().

    Try this:

    
    <?php
          if (isset($_POST['sku'])) {
            mysql_connect("localhost", "root", "");
            mysql_select_db("pennypicks");
            $ref = $_SERVER['HTTP_REFERER'];
            $msg = "<script type='text/javascript'>
    function delay() {
    setTimeout(\"window.location = 'index.php'\", 3000);
    }
    document.onload = delay();
    </script>
    Thanks for posting - Update added to mainpage!";
    
            $sku = addslashes($_POST['sku']);
            $name = addslashes($_POST['name']);
            $price = addslashes($_POST['price']);
       
            $result = mysql_query("INSERT INTO recent (sku, name, price) VALUES ('$sku', '$name', '$price');");
            if ($result) {
                echo $msg;
                exit;
            } else {
                echo "There was an error adding your entry - please try again, filling in all fields.";
            }
        }
    ?>
    
    PHP:
     
    Cozmic, Jun 17, 2010 IP
  11. mezner

    mezner Peon

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Awesome thanks a ton! Now when I also press the back button it doesn't duplicate it either! Thanks a ton!
     
    mezner, Jun 17, 2010 IP
  12. Scripts man

    Scripts man Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    its very easy just make if the posting is ok header location to the same page
     
    Scripts man, Jun 17, 2010 IP
  13. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #13
    I hope you can use the Header Redirect for this :)
     
    roopajyothi, Jun 17, 2010 IP