DevNetwork Forums Forum Index DevNetwork Forums
The Developer's Network
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Storing form values

 
Post new topic   Reply to topic    DevNetwork Forums Forum Index -> PHP - Theory and Design
View previous topic :: View next topic  
Author Message
Ree
Programmer


Joined: 10 Jun 2005
Posts: 165
Location: LT

PostPosted: Wed Sep 28, 2005 12:59 pm    Post subject: Storing form values Reply with quote

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?
Back to top
View user's profile Send private message
hawleyjr
Master


Joined: 13 Jan 2004
Posts: 719
Location: Jax FL USA

PostPosted: Wed Sep 28, 2005 1:26 pm    Post subject: Reply with quote

Store your form variables into a session variable.


A rough example:

PHP:
//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']:'';?>">

_________________
"The only 'intutive' interface is the nipple. After that, it's all learned." -Bruce Ediger
Fake it till you make it Smile
Hawleyjr.com
Back to top
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger
omega-systems
Forum Newbie


Joined: 27 Sep 2005
Posts: 14

PostPosted: Thu Sep 29, 2005 3:04 am    Post subject: Re: Storing form values Reply with quote

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
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address MSN Messenger
jayshields
Developer


Joined: 22 Aug 2005
Posts: 51
Location: Leeds, England

PostPosted: Thu Sep 29, 2005 3:17 am    Post subject: Reply with quote

imo sessions arent necessary in form processing. just combine the form processing script and the form display script into the same script using...
PHP:
<?php
if (isset($_POST['submit'])) {
     
//do something
}
?>

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.
_________________
Jay.
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
Jenk
Programmer


Joined: 19 Sep 2005
Posts: 131
Location: London

PostPosted: Thu Sep 29, 2005 4:10 am    Post subject: Reply with quote

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...
PHP:
<?php
if (isset($_POST['submit'])) {
     
//do something
}
?>

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.


I've always had to use the following when using that exact criteria:
Code:
<input type="submit" name="submit" value="Submit this form BEYOTCH!">


So I just use the name of a form element I am expecting to receive, insted. :p
Back to top
View user's profile Send private message
CoderGoblin
Master


Joined: 16 Mar 2004
Posts: 624
Location: Aachen, Germany

PostPosted: Thu Sep 29, 2005 4:25 am    Post subject: Re: Storing form values Reply with quote

omega-systems wrote:
Using session you can lose other data (user can fill two forms simultaneously).


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.
_________________
If your question has been solved, please 'edit' the initial topic's Subject title to "[Solved]Orignal Title". Thanks
Back to top
View user's profile Send private message
McGruff
Site Admin


Joined: 30 Jan 2003
Posts: 2811
Location: Glasgow, Scotland

PostPosted: Thu Sep 29, 2005 1:23 pm    Post subject: Re: Storing form values Reply with quote

Ree wrote:
Anything else you could recommend in this case?


You shouldn't need redirects to make application controller decisions.

"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.
_________________
Marcus Baker's SimpleTest: unit testing for php. http://www.lastcraft.com
Back to top
View user's profile Send private message
arborint
Programmer


Joined: 25 Aug 2004
Posts: 132

PostPosted: Thu Sep 29, 2005 4:34 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Maugrim_The_Reaper
Programmer


Joined: 02 Nov 2004
Posts: 214
Location: Ireland

PostPosted: Thu Sep 29, 2005 4:46 pm    Post subject: Reply with quote

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...
_________________
Open-Source Game Developer
http://blog.quantum-star.com
Tutorial: Challenge/Response Login Process
Back to top
View user's profile Send private message Send e-mail Visit poster's website
arborint
Programmer


Joined: 25 Aug 2004
Posts: 132

PostPosted: Thu Sep 29, 2005 6:01 pm    Post subject: Reply with quote

I didn't say submit back to the same file or page as they don't mean much in this sense. I just said submit back to itself. I assumed that it would be some kind of controller that would have multiple states/commands. The implementation may vary.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    DevNetwork Forums Forum Index -> PHP - Theory and Design All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group