Skip to content
Advertisement

How to remove form information after submit

For a school project I need to make a website that uses a database to perform queries aka a login system.

The login system fully works, but when a user for example tries to sign in with a username that doesn’t exist in the database an alert message will pop up telling the user it’s not possible. However, after that (so after the first submission) whenever I click the refresh page button, the alert will appear again without the user actually submitting any form information. So I know what the problem is, but I just don’t see where I need to unset() my variables (or something else), but at least I need to website to not show the alert if the user hasn’t submitted anything.

Here is the code and I hope someone sees where the problem is:

PHP code:

JavaScript

My form HTML code:

JavaScript

So basically the information that was pushed into the PHP variables needs to be emptied so that when the PHP code runs during a page refresh it doesn’t reuse the information that is no longer inside the form.

EDIT:

To get it working you need to use header

So after you perform the POST, I send the user to another page like this:

JavaScript

That page will tell the user if the form was correct or not and based on that sends the user to the correct page. doing this will remove the post data as the user switches to another page fixing the refresh

The working code looks something like this:

JavaScript

Advertisement

Answer

You need to remove POST header from your request. In your index.php, after your code, make a redirect to an other page. A thank you page for example.

JavaScript

Remember, header redirect must be excuted before any output.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement