preserving form input for changes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
calebsg
Forum Commoner
Posts: 28
Joined: Tue Jun 18, 2002 10:41 am

preserving form input for changes

Post by calebsg »

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?
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

This message may be useful:http://www.devnetwork.net/forums/viewtopic.php?t=1137 The last post especially seems relevant. I hope this helps.
calebsg
Forum Commoner
Posts: 28
Joined: Tue Jun 18, 2002 10:41 am

Post by calebsg »

Hmmm - thanks for the suggestion but i don't think it will work. I guess I need a way to pass the variables back and I'm trying to avoid a bunch of code editing! :lol:

Productivity and Functional Air Conditioning are Directly Proportional...
cathari
Forum Newbie
Posts: 11
Joined: Mon May 13, 2002 9:51 pm

Post by cathari »

Can't tell what's the problem but if you post snippets of your code maybe it will hep.
calebsg
Forum Commoner
Posts: 28
Joined: Tue Jun 18, 2002 10:41 am

Post by calebsg »

ok, on add_employee.php I have a form (stripped down here for simplicity)

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>
This obviously takes you over to insert_employee.php when you hit the "Add This Employee" button. insert_employee.php has the following code:

Code: Select all

if ($submit) &#123;
$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");
&#125; elseif ($verify) &#123;
<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>
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. :)
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

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
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

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.

Code: Select all

<?php

switch ( $_POST&#1111;'submit'] ) &#123;
	case 'Preview':
		displayPreview($_POST);
		displayForm($_POST);
	case 'Submit':
		processForm($_POST);
	default:
		displayForm();
		break;
&#125;

function displayPreview( $preview )
&#123;
?>
<h2>Preview</h2>
Username: <?=$preview&#1111;'username']?><br />
Email: <?=$preview&#1111;'email']?><br />
Password: <?=$preview&#1111;'password']?><br />
If everything is okay, you can submit the data.
<hr />
<?
&#125;

function displayForm ( $data=false )
&#123;
?>
<form action="<?=$GLOBAL&#1111;'PHP_SELF']?>" method="post">
	Username: <input type="text" name="username" value="<?=$data&#1111;'username']?>"><br />
	Email: <input type="text" name="email" value="<?=$data&#1111;'email']?>">
	Password: <input type="password" name="password" value="<?=$data&#1111;'password']?>">
	<input type="submit" name="submit" value="Submit">
	<input type="submit" name="submit" value="Preview">
</form>
<?
&#125;

function processForm ( $data )
&#123;
	// 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. =)
&#125;

?>
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.)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

btw. where has the php-tag-button gone?
highlighted code was much easier to read ;)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Lost it in the upgrade...

I will put it back... :D
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

You have to start think of PHP as programming, and not as seperate webpages.
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.

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.
calebsg
Forum Commoner
Posts: 28
Joined: Tue Jun 18, 2002 10:41 am

Post by calebsg »

I think Bill H has a good point -- there's a lot of enterprising people out there who are piecing together websites from bits of code while never really understanding the fundamental concepts of programming because those types of tutorials simply don't exist or are too laborious to read.
cathari
Forum Newbie
Posts: 11
Joined: Mon May 13, 2002 9:51 pm

Post by cathari »

Well done Jason. I think maybe you should always be in a good mood to explain things it helps a lot to people like us [' Newbie ']. Maybe you should include some tutorials in this site in order to keep the messages from being post again and again.
cathari
Forum Newbie
Posts: 11
Joined: Mon May 13, 2002 9:51 pm

Post by cathari »

Well done Jason. I think maybe you should always be in a good mood to explain things it helps a lot to people like us [' Newbie ']. Maybe you should include some tutorials in this site in order to keep the messages from being post again and again.
Post Reply