Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing form data after submisssion in PHP

Tags:

php

I have been stuck up at the point of form data clearing. After the user submits a form the data is added to database. Its fine till here. But if the user refreshes the page previously entered data is re entered into the database. I am using post method to send the data. I am not using AJAX. The whole page reloads after the for data is submitted. Please help me with this. If there is any way to clear form data after submiting it, so that after refreshing the page no data is added to database do reply to this question.

like image 681
Sanket Raut Avatar asked Sep 29 '10 13:09

Sanket Raut


1 Answers

After inserting the data, do a redirect:

header('Location:'.$_SERVER['PHP_SELF']);

After redirecting, refreshing won't cause data to be inserted again.


To show the message to the user I would simply set a flag via $_GET. For example:

// Redirect and set $_GET variable
header('Location:'.$_SERVER['PHP_SELF'].'?showmsg=true');

To show the hidden div you could make a function:

function show_confirmation()
{
    return isset($_GET['showmsg']) && $_GET['showmsg'] == 'true';
}

And use the function like this:

<div style="display:<?php echo show_confirmation() ? 'block' : 'none'; ?>">
    <!-- Message for user -->
</div>
like image 134
Mischa Avatar answered Sep 23 '22 17:09

Mischa