0

I am developing a courier application using PHP and MySQL and I have come across a minor bug. Say, I have a page that adds a new shipment (add.php). Upon filling the details and clicking on "submit" button in the form, addshipment.php is fired which contains the code to add the new shipment to the sql table. If the data is entered successfully, the following code will execute:

header("location:add.php?add=success");

Thus, the add.php page will reload with the URL "add.php?add=success" with an alert box that will say that data has been inserted successfully. That alert box is executed via the following code at the bottom of the page:

if(isset($_GET['add']))
{
    switch($_GET['add'])
    {
        case "invalid":
            echo "<script>alert('Please fill all the fields');</script>";
            break;
        case "fail":        
            echo "<script>alert('Your data was not inserted successfully');</script>";
            break;
        case "success": 
            echo "<script>alert('Your Data was Added Successfully');</script>";
            break;
    }
}

Works fine but every time I refresh the page I get the same alert box since the URL still contains ?add=success. I wish the add.php page not to contain the values after data insertion but still display the alert message. Any ideas?

Thanks

PravinS
  • 2,640
  • 3
  • 21
  • 25
  • You need to reset your header part after alerting data – Narendrasingh Sisodia Aug 01 '15 at 09:07
  • possible duplicate of [PHP Redirection with Post Parameters](http://stackoverflow.com/questions/2865289/php-redirection-with-post-parameters) also http://stackoverflow.com/questions/4281900/php-header-redirect-with-post-variables – Jigar Aug 01 '15 at 09:09
  • Do you really make a fill reload? This looks ugly, slow and causes such problems. Why don't you send the request by means of an ajax request instead and display the success message inside the already loaded page? – arkascha Aug 01 '15 at 09:13
  • you can always add them to the $_SESSION or use another get variable. or change add=success to add=pending, display message, then change it to success -via- `header('Location: ...` – ArtisticPhoenix Aug 01 '15 at 09:54
  • do a redirect to any other page that does not contain the add form – john Smith Aug 05 '15 at 12:21

3 Answers3

0

Replace success in the URL with a message ID.

Store the message in a database, associated with the ID.

After the ID has been requested, generate the page with the alert and then mark the message in the database as seen (or delete it entirely).

If another request for the same ID comes in, don't include the alert (or redirect to the URL with the query string on the end).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

The common solution for this issue is to store messages in session and remove them once displayed. Many frameworks have appropriate mechasnim included (eg. Zend Framework has FlashMessenger).

You may also create such mechanism on your own, it's pretty simple. The most basic usage may look as follows:

// put any message in $_SESSION['message'] BEFORE redirection
$_SESSION['message'] = 'success';

header("location:add.php");

And then at the bottom of the page:

if(isset($_SESSION['message']))
{
    switch($_SESSION['message']) {
        case "invalid":
            echo "<script>alert('Please fill all the fields');</script>";
            break;
        case "fail":        
            echo "<script>alert('Your data was not inserted successfully');</script>";
            break;
        case "success": 
            echo "<script>alert('Your Data was Added Successfully');</script>";
            break;
    }

    // do not display this message again
    unset($_SESSION['message']); 
}

Don't forget to call session_start somewhere at the top of your code.

ptkoz
  • 2,388
  • 1
  • 20
  • 28
-1

We need to use name for submit button. It's need to get values from the form if submit button is click. Then we can check whether we click the submit button. Assume name of submit button is "submit". Then:

if (isset($_POST[submit]))
{
  /* do the form validation and processing here. */ 
}

This is the way we can check we see this page for the first time or whether we click submit button. URL will not change. Therefore alert message wont pop-up when we refresh the page. Just use $_POST[] instead of $_GET[].