0

Form get's resent on refresh, I had read about header("Location:my_page.php") unset($_POST), but I'm not sure where to place it.

This is our script, it works as need it, but it keeps re-sending on page refresh (Chrome browser alerts over and over), can some one fix the code and explain to my like 2 years old child.

<form action='thi_very_same_page.php' method='post'>
Search for Christian Movies <input type='text' name='query' id='text'  />
<input type='submit' name='submit' id='search' value='Search' />
</form>

<?php
if (isset($_POST['submit'])) 
{
    mysql_connect("localhost", "root", "toor") or die("Error connecting to database: " . mysql_error());
    mysql_select_db("db_name") or die(mysql_error());
    $query = $_POST['query'];

    $min_length = 2;
    if (strlen($query) >= $min_length) 
    {
        $query = htmlspecialchars($query);
        $query = mysql_real_escape_string($query);
        echo "";

        $result = mysql_query("SELECT *, DATE_FORMAT(lasteditdate, '%m-%d-%Y') AS lasteditdate  FROM movies WHERE (`moviename` LIKE '%" . $query . "%') OR (`year` LIKE '%" . $query . "%')") or die(mysql_error());

        if (mysql_num_rows($result) > 0) 
        {
            while ($results = mysql_fetch_array($result)) 
            {
                echo "";
            }
        }
        else
        {
            echo "";
        }
    } 
    else 
    {
        echo "";
    }
}
Monica
  • 1
  • 1
  • Don't use mysql* use PDO instead. – Athar Ahmed Aug 11 '13 at 10:22
  • I'm new to this whole php scripts, not even know how use this simple one, now that PDO seems to be a whole new story :/ – Monica Aug 11 '13 at 10:32
  • see http://php.net/manual/en/book.pdo.php and http://php.net/manual/en/intro.pdo.php – Athar Ahmed Aug 11 '13 at 10:34
  • Your question is not clear try to explain your problem in detail and currently your code echos nothing – M Khalid Junaid Aug 11 '13 at 10:44
  • I had edit the main question, added the input form to it, now the issue is: If my college mates, do enter a criteria (movie name), if hit f5, page alerts (The Page you're looking for used information that you entered. Returning to that page migth cause any action took to be repeated, Do you wnat to contine?) ... That is what happens :( – Monica Aug 11 '13 at 11:06

2 Answers2

0

If you mean that the form data gets submitted again upon refresh, check this method

http://www.andypemberton.com/engineering/the-post-redirect-get-pattern/

You set your header to header('HTTP/1.1 303 See Other');

Data wont be cached, so when page refreshes the form data wont get submitted again!

meWantToLearn
  • 1,691
  • 2
  • 25
  • 45
  • Thanks for the link, my english is not that good, need it time to read that whole page, now about the header set to HTTP/1, how to do it ?, Thanks :* – Monica Aug 11 '13 at 10:35
  • you write header('HTTP/1.1 303 See Other'); in your code that does the processing of data before redirecting it to the same page – meWantToLearn Aug 11 '13 at 15:41
0

The problem is you are using the post method to submit the form values so when ever you tries to refresh the browser asks you whether to send the form information or not it is the default behavior of the browser to tackle the posted information, the alternate solution for your problem is you can use the get method like in form attribute method='get' what it does it will append all the information of form in the URL which we call the query string and in PHP code you are accessing the form values in $_POST but when using get method the form values will now appear in the $_GET method these methods are called request method and are PHP's global variables, Now when you try to refresh it will not ask you to resend information because the information now resides in the URL

<form action='thi_very_same_page.php' method='get'>
Search for Christian Movies <input type='text' name='query' id='text'  />
<input type='submit' name='submit' id='search' value='Search' />
</form>

<?php
if (isset($_GET['submit'])) 
{
    mysql_connect("localhost", "root", "toor") or die("Error connecting to database: " . mysql_error());
    mysql_select_db("db_name") or die(mysql_error());
    $query = $_GET['query'];

    $min_length = 2;
    if (strlen($query) >= $min_length) 
    {
        $query = htmlspecialchars($query);
        $query = mysql_real_escape_string($query);
        echo "";

        $result = mysql_query("SELECT *, DATE_FORMAT(lasteditdate, '%m-%d-%Y') AS lasteditdate  FROM movies WHERE (`moviename` LIKE '%" . $query . "%') OR (`year` LIKE '%" . $query . "%')") or die(mysql_error());

        if (mysql_num_rows($result) > 0) 
        {
            while ($results = mysql_fetch_array($result)) 
            {
                echo "";
            }
        }
        else
        {
            echo "";
        }
    } 
    else 
    {
        echo "";
    }
} ?>

Hope this is enough to explain you about the form submission one thing I will suggest you to deeply look at below


Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118