Keep form variables

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
ruth
Forum Newbie
Posts: 19
Joined: Wed Aug 07, 2002 9:24 pm
Location: Melbourne, Australia

Keep form variables

Post by ruth »

Hello everyone,
I am developing a web application with php/mysql. The application has many forms, and each form has 30 to 80 fields. The problem is that once the user submits a form, all form inputs are lost if the user wants to go back to the form by using Back button. This is frustrating because the user has to enter all fields again once the submission fails. I noticed this is happeing because I used authentication to protect the form page. If the user has not logged in, the page is rediected by "header(location: 'login-form')". I tried to define the field values as variables as bellow, but it didn't help.

Code: Select all

<input type="text" name="Name" value="<?php echo $FirstName; ?>" size="10" maxlength="30">
I have to solve this problem, otherwise, the application will not be acceptable by the client :cry: . Please help ... I would appreciate any suggestions.

Thanks in advance.

Ruth
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

the way i handle these things is to load the form data into a session and then for each input box i have something like:

Code: Select all

<input type="text" name="Name" value="<?php echo $_SESSION&#1111;'FirstName']; ?>" size="10" maxlength="30">
this usually works for me.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Another thing you can do is submit the form back to itself with a different sub-routine that runs when it was submitted. If there is a problem with the submission, it shows the form again and you allready have all the vars to echo back into those fields.

Cheers,
BDKR
ruth
Forum Newbie
Posts: 19
Joined: Wed Aug 07, 2002 9:24 pm
Location: Melbourne, Australia

Post by ruth »

Thank you very much for your reply to my post. Unfortunately, I have not solved my problem.

mydimension: I am a bit worried about using sessions because I have so many fields on a form, and many forms too. In that case, I will need to register too many session variables. Not sure if using an array to store all inputs and put it in a session would make it better. I will give it a try.

BDKR: I also tried to submit the form back to the same page, but it did not work either. I did some thing like:

Code: Select all

if($_POST&#1111;'action']=='save')
&#123;
    do_some_thing_here();
&#125;
else
&#123;
   display_HTML_form();
&#125;
I don't want the form to be displayed again after the submission until the user presses browser's Back button. But I guess in this case the form can not grab the posted inputs back to the fields.

I am sure this is a common problem, and wonder if I have other options.

Thank you very much.
ruth
Forum Newbie
Posts: 19
Joined: Wed Aug 07, 2002 9:24 pm
Location: Melbourne, Australia

Post by ruth »

I have just found a solution: use session_cache_limiter('private') before calling session_start(). It works perfectly.

Thank you for your support.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Good! I'm glad you found something that works.

Cheers,
BDKR
jasonn
Forum Newbie
Posts: 4
Joined: Mon Nov 04, 2002 9:52 am
Location: Utah

Post by jasonn »

I am having the same problem. Very Annoying!

I tried the

Code: Select all

session_cache_limiter("private")
but got nowhere. I tried placing it on the page with the form, the page that the form submitted to, and on both. Nothing is working.

Is there some other way?

jasonn.
ruth
Forum Newbie
Posts: 19
Joined: Wed Aug 07, 2002 9:24 pm
Location: Melbourne, Australia

Post by ruth »

Jasonn,
My porblem was caused by the use of session variables. So, in my case, session_start() is called at top of a page. Therefore, session_cache_limiter("private") must be the first line on the page. If your problem is not due to the session. This may not solve your problem. Have you tried to use variables for the field values? Some thing like:

Code: Select all

<form method="post" action="action.php">
<input type="text" name="Name" value=<?php echo $Name; ?> >
something_else
</form>
If you can not use the above to solve your problem. I suggest that you use a simple test page just to find out what is the cause of the problem.

Ruth
jasonn
Forum Newbie
Posts: 4
Joined: Mon Nov 04, 2002 9:52 am
Location: Utah

Post by jasonn »

I will give that a try.

Thanks,
jasonn.
Post Reply