Fri, 17 May 2024


PHP Header Function

By: Anton, NetArt Media
Wed, 1 August 2018
PHP Header Function

If you're having issues with visitors leaving your website because they are sometimes forced to resubmit a form they have already processed then this PHP Tool Box article may help you out. Javascript and AJAX can be quite helpful in situations like this, but don't discount PHP when it comes to tackling this particular issue.

Below, we have a code block that first grabs the tail end of any url the visitor is currently viewing. For instance, if the full url is 'website.com/form.php', the basename function will pick up 'form.php' and act accordingly.

The conditional statement is part of a number of conditions which check to see if the form has been filled in properly. In the event the end-user either forgot to enter in a username or an email address they are shown an error message for 2 seconds and then the page will refresh.


As helpful as this is however, you may frustrate your visitors yet again by emptying the form during the refresh. So using sessions to save the data that has already been entered would make their experience more friendly.


In your HTML form you would then make sure the form values will be shown during the session in the event information has been entered into the fields and the page refreshes due to an error occurring.


Now, this method works fine when doing the error processing on the same page, but once a form is successfully submitted it's usually good practice to send them to another page where they can be met with a friendly message and then continue browsing your site as they wish. Let's take a look at how we would update the PHP code for this method.


When the form is successfully filled out and submitted we can then send the end-user to the form processing page where they will see a message.


This is an idea of what we would place at the top of the form processing page. We check to see if the form has been submitted, as well as the username and email variables. In the event everything is fine we define a message for the end-user. We display that within our HTML code using the snippet below.


This is pretty much the gist of dealing with end-users having to resubmit a form they don't want to. Of course, there are more Conditional Statements to include within the PHP code if there are more variables to check for etc, but this is a good start. Furthermore, by using sessions we make things slightly safer than if we passed information through a URL via the GET function.

I hope this PHP Tool Box article may shed some light on how to deal with processing forms using PHP without giving the end-user too much grief. They will actually stick around your website this way, which could be the start of a beneficial relationship for both of you.


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox