Storing form values
Moderator: General Moderators
Storing form values
I usually handle form input this way: some form is on a form_page.php and POST goes to some other script (indicated in the "action" tag), for example, form_processor.php. If there was any problem with submitted data (empty/invalid fields), form_processor.php would redirect back to form_page.php and an appropriate message would be displayed. The problem is that upon redirection back to form_page.php, all form fields would be completely empty. I don't want the user to refill ALL the form fields if he made a mistake somewhere. I need the last submitted values stay after redirection. The only solution I can think of right now is sessions/cookies. Anything else you could recommend in this case?
Store your form variables into a session variable.
A rough example:
A rough example:
Code: Select all
//PROCESS PAGE
$_SESSION['FORM_VARS'] = $_POST;
//FORM PAGE
<input type="text" name="my_txt" value="<?php echo isset($_SESSION['FORM_VARS']['my_txt'])?$_SESSION['FORM_VARS']['my_txt']:'';?>">-
omega-systems
- Forum Newbie
- Posts: 14
- Joined: Tue Sep 27, 2005 5:01 am
- Contact:
Re: Storing form values
Using session you can lose other data (user can fill two forms simultaneously). Thats'why better use for processing and imagine same script. If user enter incorrect data or can't fill all fileds, you'll able to redisplay them.
Regards,
Michael.
Project Manager
Omega Systems Ltd
Email: info@omega-systems.biz
ICQ: 264962449
MSN: omega-systems@hotmail.com
AIM: OmegaSys Ltd
Regards,
Michael.
Project Manager
Omega Systems Ltd
Email: info@omega-systems.biz
ICQ: 264962449
MSN: omega-systems@hotmail.com
AIM: OmegaSys Ltd
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
imo sessions arent necessary in form processing. just combine the form processing script and the form display script into the same script using...
to put the processing info into it. then it will keep all the post values because you will be redirecting to the same page. then just set the values of the inputs to the post values if they have already been set.
Code: Select all
<?php
if (isset($_POST['submit'])) {
//do something
}
?>I've always had to use the following when using that exact criteria:jayshields wrote:imo sessions arent necessary in form processing. just combine the form processing script and the form display script into the same script using...to put the processing info into it. then it will keep all the post values because you will be redirecting to the same page. then just set the values of the inputs to the post values if they have already been set.Code: Select all
<?php if (isset($_POST['submit'])) { //do something } ?>
Code: Select all
<input type="submit" name="submit" value="Submit this form BEYOTCH!">- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
Re: Storing form values
Depends on how it is done. It is even more important to keep track of this now with tabbed browsers. I tend to give all my forms a unique identifier in the session. saving the form saves the information to the $_SESSION['form']['identifier]=values. On a sucessful save the form's values are emptied to prevent another save. This works quite well.omega-systems wrote:Using session you can lose other data (user can fill two forms simultaneously).
Re: Storing form values
You shouldn't need redirects to make application controller decisions.Ree wrote:Anything else you could recommend in this case?
"Application controller" logic is something which decides the flow of the application based on the state of something or other. In a script which deals with a form submission, this means deciding if the submission is processable (all required fields were filled in, and all values have validated) or if the form should be redisplayed (they're not). You'd want to redirect after processing a submission to avoid the refresh-resubmit problem but you don't need to do this for the resubmit case - as you've found it creates a complication in that you have to persist the submission somehow in order to redisplay submitted values.
In OOP, I'd have a couple of request handler classes: one for form display which echo's back any POST values to the form (on first display POST is empty so you get an empty form) and one to process a valid submission.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
The most obvious thing to do is to not submit form_page.php to form_processor.php, but instead have the form processor submit to itself. It needs to support init, submit and done states, but it is the standard way to do it. The usual setup is to have the form submit to itself and then redirect when done. That solves both the problem of retaining values and the Back button problem as well.
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
See McGruff's post...
Yes, you can submit back to the same file - but that does not make sense in all cases. For example I run an application which utilises a Page Controller and some basic MVC. I do not want the form to submit to the same page - but rather though a page controller to a separate command object...
Your case is only standard in limited circumstances...
Yes, you can submit back to the same file - but that does not make sense in all cases. For example I run an application which utilises a Page Controller and some basic MVC. I do not want the form to submit to the same page - but rather though a page controller to a separate command object...
Your case is only standard in limited circumstances...
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Well, now I have the form display and data processing on the same .php script which allows me to redisplay submitted values in the form in case some of them didn't pass validation. This is all fine but I'm wondering if that 'POST' popup message in FF (when trying to refresh the page) is all right. Well, it asks if you want to resend the data, so I guess in my case it's fine.