preserving form input for changes
Moderator: General Moderators
preserving form input for changes
I've searched all over this forum in vain for the answer to this, cause I figure someone has got to have asked this one before.
Nevertheless....
Simple issue: someone puts stuff in a form on page 1, page 2 displays their input and says are you sure? If yes, it's inserted into db. That much I've done. Now if they see an error on page 2 in their input and hit the back button, 'poof' all their input is gone. How can I make sure it's there if they hit the back button to edit something?
Nevertheless....
Simple issue: someone puts stuff in a form on page 1, page 2 displays their input and says are you sure? If yes, it's inserted into db. That much I've done. Now if they see an error on page 2 in their input and hit the back button, 'poof' all their input is gone. How can I make sure it's there if they hit the back button to edit something?
This message may be useful:http://www.devnetwork.net/forums/viewtopic.php?t=1137 The last post especially seems relevant. I hope this helps.
ok, on add_employee.php I have a form (stripped down here for simplicity)
This obviously takes you over to insert_employee.php when you hit the "Add This Employee" button. insert_employee.php has the following code:
So you punch the "Looks Good" button on this page and it inserts into the database. Whoopee!
The problem is what if you messed up the first name? How do I go back to the form on the previous page and edit the first name? If you hit the "Back" button all your data is gone (in real life this is a fairly lengthy form) and you have to reenter everything from scratch. Ideally you could hit the back button and everything would still be there. How do I make that happen? Or should I be doing it differently?
Thanks for your efforts -- I do appreciate the help this forum gives.
Code: Select all
<form action="insert_employee.php" method="post">
First Name: <input type="text" name="first_name" class="tx" size="35" maxlength="30">
<input type="submit" value="Add This Employee" name="verify" class="tx">
</form>Code: Select all
if ($submit) {
$mysql = "INSERT INTO employee_info (em_first_name) VALUES ('$first_name')" or die ("error in query");
$myquery = mysql_query($mysql) or die ("error in result");
header("Location: aed_employee.php");
} elseif ($verify) {
<form action="insert_employee.php" method="post">
First Name:
<input type="hidden" name="first_name" value="<? echo $first_name;?>"><?echo $first_name; ?>
<input type="submit" value="Looks Good - Save Info" name="submit" class="tx">
</form>The problem is what if you messed up the first name? How do I go back to the form on the previous page and edit the first name? If you hit the "Back" button all your data is gone (in real life this is a fairly lengthy form) and you have to reenter everything from scratch. Ideally you could hit the back button and everything would still be there. How do I make that happen? Or should I be doing it differently?
Thanks for your efforts -- I do appreciate the help this forum gives.
- Bill H
- DevNet Resident
- Posts: 1136
- Joined: Sat Jun 01, 2002 10:16 am
- Location: San Diego CA
- Contact:
Have you thought about putting the values into session varaiables?
Blank the session variables in page 1 -- header to page 2 (the form)
Set the session vlues (blank at this point) into the user inputs on page 2
Put the user inputs into the session variables in page 3 which also gets user confirmation
"Back" loads the session values into user inputs in page 2
Blank the session variables in page 1 -- header to page 2 (the form)
Set the session vlues (blank at this point) into the user inputs on page 2
Put the user inputs into the session variables in page 3 which also gets user confirmation
"Back" loads the session values into user inputs in page 2
Your lucky I am in a good mood. =) You have to start think of PHP as programming, and not as seperate webpages.
To accomplish what you are wanting to do, it's very very simple to do on one page.
The above is easily structured, seperated, and gives you a clear understanding of how to accomplish what you need. The first time the page loads, only displayForm() runs. The second time the page loads (when the person does a "Preview" for example, the page show the data, as well as the form. This makes it easy for a person to not only preview the data, but make changes right then and there. No need to make the person do extra leg work. In fact, the phpBB preview button on posts works the same way, showing you your post above, and the form below.
Your want to do it via the back button is sloppy (not your fault, you didn't know a better way), and the method above I have found to work for me for ages. I hope it helps.
Note: I didn't test this, but the logic is there, so forgive possible syntax errors. I am running on Homesites syntax highlighting =). I just wrote this, but I know it works (I have done it enough times before.)
To accomplish what you are wanting to do, it's very very simple to do on one page.
Code: Select all
<?php
switch ( $_POSTї'submit'] ) {
case 'Preview':
displayPreview($_POST);
displayForm($_POST);
case 'Submit':
processForm($_POST);
default:
displayForm();
break;
}
function displayPreview( $preview )
{
?>
<h2>Preview</h2>
Username: <?=$previewї'username']?><br />
Email: <?=$previewї'email']?><br />
Password: <?=$previewї'password']?><br />
If everything is okay, you can submit the data.
<hr />
<?
}
function displayForm ( $data=false )
{
?>
<form action="<?=$GLOBALї'PHP_SELF']?>" method="post">
Username: <input type="text" name="username" value="<?=$dataї'username']?>"><br />
Email: <input type="text" name="email" value="<?=$dataї'email']?>">
Password: <input type="password" name="password" value="<?=$dataї'password']?>">
<input type="submit" name="submit" value="Submit">
<input type="submit" name="submit" value="Preview">
</form>
<?
}
function processForm ( $data )
{
// Take the data and actually process this data. After that
// simply use header("Location: something.php") to a result page
// after they submit data. This also prevents people from accidently
// entering the same data twice by reloading the page. =)
}
?>Your want to do it via the back button is sloppy (not your fault, you didn't know a better way), and the method above I have found to work for me for ages. I hope it helps.
Note: I didn't test this, but the logic is there, so forgive possible syntax errors. I am running on Homesites syntax highlighting =). I just wrote this, but I know it works (I have done it enough times before.)
- Bill H
- DevNet Resident
- Posts: 1136
- Joined: Sat Jun 01, 2002 10:16 am
- Location: San Diego CA
- Contact:
That statement and the technique that accompanied it are just a huge treasure. It is so much more elegant than what I've been doing in so many ways that it simply beggars description.You have to start think of PHP as programming, and not as seperate webpages.
I have been programming quite a few years, long enough to not enjoy using a hemmer when a gentle tap will do. I'm very new to PHP and am reading forums voraciously to learn better approaches.
This one paid off big. Thank you so much.