I have an HTML form page which I post back to itself in order to perform some PHP Validation.
Once posted back to itself and the validation passes, I direct the page to the next link using the string:
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.website.com/planning/page2.php\">";
When using this however the page refreshes on itself before going to the next page (page2.php) this means the page flashes again whilst performing validation then redirects. How can I make this not ‘flash’ when performing the validation?
my validation is as follows:
//validation
if(isset($_POST['loadid'])){
$errors = array();
$load=$_POST['loadid'];
if (empty($load)){
$errors[]='<strong><font color=red>Please select a load number</font></strong>';
}
if (!empty($errors)) {
foreach ($errors as $error) {
echo $error."<br>";
}}
else {
echo "Population accurate";
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.website.com/planning/page2.php\">";
$_SESSION['sessionloadid']=$_POST['loadid'];
}
}
Thanks for the help,
Ryan Smith
The flash results because you’re doing the redirect “inside the browser”. That means, the browser needs to load your HTML and then already starts to display it.
Then the browser sees it should redirect, resets the window (flash) and does the redirect.
You can prevent this by doing the redirect already with HTTP headers:
instead of:
A browser processes HTTP headers before starting to display the page, it then sees the redirect and redirects (often automatically). If the browser does not redirect automatically, a short “Moved.” message is displayed, so the user can navigate to the new location by clicking the link.
See as well the
http_redirectfunction, that offers more to make redirects with PHP. If you don’t have the extension available, the function documentation shows multiple points you should take care about when doing redirects with PHP:If you don’t have that function available, I suggest you write one that does the same, so you can easily do redirects within your application.