Originally posted by Zebbedy
Im getting tired of having to re-enter data after pressing 'back' when i have checked a form for user errors. Is there a php function that will allow the restoration of data fields when 'back' has been pressed?
Is there a better way?
I have last 2-3 days been writing a simple set of pages
to submit form data and store, remember, entered data in SESSION
There are several other ways. But this an example how you can do it.
You can use this set of pages, but add and modify
to get it to suit your needs. Or write something like it:
// Script by halojoy sweden june 2005
// Join my website and submit your user name
<!-- //////////////////////////////-->
<?php
// home.php
session_start();
$user='Guest';
if(isset($_SESSION['name']) && !empty($_SESSION['name'])){
$user=$_SESSION['name'];
echo "Welcome home <b>$user</b>";
}
else
echo "Welcome <b>$user</b>";
if($user=='Guest')
echo "<br><br><a href='page1.php'>Join this website</a>";
else
echo "<br><br><a href='page1.php'>Change my profile</a>";
?>
<!-- //////////////////////////////-->
<?php
// page1.php
// session hasto be started when store or read
session_start();
if ( !isset($_SESSION['name']) || $_POST['reset'] ) $_SESSION['name'] = '';
if ( !isset($_SESSION['pass']) || $_POST['reset'] ) $_SESSION['pass'] = '';
if ($_POST['submit']) {
$usern = trim($_POST['username']);
$passw = trim($_POST['password']);
if ($usern=='?') $usern='';
if ($passw=='?') $passw='';
if(!empty($usern)) $_SESSION['name'] = $usern;
if(!empty($passw)) $_SESSION['pass'] = $passw;
}
$usern = '?';
$passw = '?';
if(!empty($_SESSION['name'])) $usern = $_SESSION['name'];
if(!empty($_SESSION['pass'])) $passw = $_SESSION['pass'];
?>
<!-- when action empty, goes back this page again -->
<form action='' method='post'>
<input type='text' name='username' value='<?php echo $usern ?>'> Username
<br>
<input type='text' name='password' value='<?php echo $passw ?>'> Password
<br>
<input type='submit' name='submit' value='SUBMIT'>
<input type='submit' name='reset' value='RESET'>
<br><br>
<a href='home.php'>Cancel</a>
<br>
<a href='page2.php'>Continue</a>
<!-- //////////////////////////////-->
<?php
// page2.php
// hasto start SESSION
session_start();
$uname = $_SESSION['name'];
$upass = $_SESSION['pass'];
echo "User Name: $uname";
echo '<br>';
echo "Pass Word: $upass";
?>
<br><br>
<a href='page1.php'>Back</a>
<br>
<a href='home.php'>Cancel</a>
<br>
<a href='page3.php'>Continue</a>
<!-- //////////////////////////////-->
<?php
// page3.php
echo "This is";
echo '<br>';
echo "The End";
?>
<br><br>
<a href='home.php'>Home</a>
Pages.
-----------------------------------
home.php - startpage
page1.php - where the first submit form is
page2.php - you can add a 'next step' here
page3.php - The End 'step'
π