Possibly stupid question time:
On many websites, when a user submits a form, the information is checked, and if it is incorrect they are redirected back to the form, with all/most of the information still intact and entered into the form. E.g. they type in the wrong password, but after the redirect, their username is still intact in the form. I want to know the best method in which this can be done.
I did some testing:
post1.php:
<?php
$_POST['user']='name';
header('Location: post2.php');
?>
post2.php:
<?php
if(!empty($_POST['user'])){
header('Location: post3.php');
//echo 'post: '.$_POST['user'];
}
else{
echo 'nope post2.';
}
?>
post3.php:
<?php
if(!empty($_POST['user'])){
echo 'post: '.$_POST['user'];
}
else{
echo 'nope post3.';
}
?>
When using a form on post1.php, I could get post3.php to echo 'nope post3.'
which shows that post values don't stick around after a redirect. However, when using the current version of post1.php, I could only get post2.php to echo 'nope post2.'
, meaning that setting $_POST variables manually doesn't work, or I could on the validation page enter $_POST['var']=$_POST['var']
and it would save the post variable through one more redirect.
I know this could be done using SESSION variables, but I feel like that could get messy quickly.
and before you comment about security risks, I obviously wouldn't do this with any sensitive information.
tl:dr; How do I best send variables back to the page the form is on after form validation fails?