<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: Retaining form data when going back and still using PHP sessions.</title>
	<link>http://ask.metafilter.com/83127/Retaining-form-data-when-going-back-and-still-using-PHP-sessions/</link>
	<description>Comments on Ask MetaFilter post Retaining form data when going back and still using PHP sessions.</description>
	<pubDate>Fri, 08 Feb 2008 06:10:36 -0800</pubDate>
	<lastBuildDate>Fri, 08 Feb 2008 06:10:36 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: Retaining form data when going back and still using PHP sessions.</title>
		<link>http://ask.metafilter.com/83127/Retaining-form-data-when-going-back-and-still-using-PHP-sessions</link>	
		<description>When I altered a PHP web form to take advantage of PHP Sessions, I lost the ability to go back a page while retaining data in the form. Is this the expected behaviour, or is it caused by something else? &lt;br /&gt;&lt;br /&gt; There are two pages, &lt;strong&gt;newentry.php&lt;/strong&gt; and &lt;strong&gt;processentry.php&lt;/strong&gt;. The first page contains an HTML form whose action is the second page.&lt;br&gt;
&lt;br&gt;
The second page does some error checking and alerts the user to go back if something is not correct. I have attempted both javascript:history.back, history.go(-1), and direct links to newentry.php. Before I added Session support, going back to the form returned you to a page with the data still filled in.&lt;br&gt;
&lt;br&gt;
Since I added sessions, going back takes you to a blank copy of the form. Is this normal? Is there some other cause I should investigate?&lt;br&gt;
&lt;br&gt;
What is the best way to handle this sort of thing? Should I not rely on the first behaviour at all?</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2008:site.83127</guid>
		<pubDate>Fri, 08 Feb 2008 05:36:34 -0800</pubDate>
		<dc:creator>odinsdream</dc:creator>
		
			<category>html</category>
		
			<category>form</category>
		
			<category>php</category>
		
			<category>sessions</category>
		
			<category>programming</category>
		
			<category>web</category>
		
			<category>webdev</category>
		
	</item> <item>
		<title>By: bricoleur</title>
		<link>http://ask.metafilter.com/83127/Retaining-form-data-when-going-back-and-still-using-PHP-sessions#1231124</link>	
		<description>Could you be more specific about what you mean by &quot;added sessions&quot;?</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.83127-1231124</guid>
		<pubDate>Fri, 08 Feb 2008 06:10:36 -0800</pubDate>
		<dc:creator>bricoleur</dc:creator>
	</item><item>
		<title>By: DreamerFi</title>
		<link>http://ask.metafilter.com/83127/Retaining-form-data-when-going-back-and-still-using-PHP-sessions#1231142</link>	
		<description>My first guess would be to look at the code in newpage.php that generates new sessions. If every visit to that page creates a new session, and you pre-fill the fields form the session variable, you&apos;ll get an empty page every time.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.83127-1231142</guid>
		<pubDate>Fri, 08 Feb 2008 06:28:04 -0800</pubDate>
		<dc:creator>DreamerFi</dc:creator>
	</item><item>
		<title>By: missmagenta</title>
		<link>http://ask.metafilter.com/83127/Retaining-form-data-when-going-back-and-still-using-PHP-sessions#1231148</link>	
		<description>I&apos;m assuming you&apos;re trying to repopulate the form using session data. Do both pages contain session_start(); at the top? Are you populating the session with the form data?&lt;br&gt;
&lt;br&gt;
As bricoleur says, you need to be more specific about what you&apos;ve done. The code would be handy.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.83127-1231148</guid>
		<pubDate>Fri, 08 Feb 2008 06:30:13 -0800</pubDate>
		<dc:creator>missmagenta</dc:creator>
	</item><item>
		<title>By: bertrandom</title>
		<link>http://ask.metafilter.com/83127/Retaining-form-data-when-going-back-and-still-using-PHP-sessions#1231160</link>	
		<description>Kinda guessing without seeing the code, but if you have register_globals on, session variables become real variables automatically.&lt;br&gt;
&lt;br&gt;
The refilling of the form when you hit back is actually done by the user&apos;s browser, not PHP. And no, you shouldn&apos;t rely on this, you should have the code check if everything is filled in and if not, redisplay the form on the first page, this time putting the error at the top of it. If you want to prevent the user even having to submit the form, there&apos;s some very simple Javascript client-side validation you can do, but you always have to do the server-side validation.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.83127-1231160</guid>
		<pubDate>Fri, 08 Feb 2008 06:44:13 -0800</pubDate>
		<dc:creator>bertrandom</dc:creator>
	</item><item>
		<title>By: malevolent</title>
		<link>http://ask.metafilter.com/83127/Retaining-form-data-when-going-back-and-still-using-PHP-sessions#1231177</link>	
		<description>You can sometimes get away with relying on the browser to hang onto form input for you but, as you&apos;re finding, it&apos;s not ideal (I think the browser makes a judgement as to whether the page should be retained based on headers etc., and different browsers will probably apply different rules).&lt;br&gt;
&lt;br&gt;
The best solution is to make the form submit back to the same page, so if something goes wrong it can all be properly prepopulated. There are numerous PHP-based classes/frameworks for making forms easier by filtering/validating/rendering when given a description of the form (not sure which ones to recommend as I went the NIH route and created my own).</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.83127-1231177</guid>
		<pubDate>Fri, 08 Feb 2008 06:57:35 -0800</pubDate>
		<dc:creator>malevolent</dc:creator>
	</item><item>
		<title>By: le morte de bea arthur</title>
		<link>http://ask.metafilter.com/83127/Retaining-form-data-when-going-back-and-still-using-PHP-sessions#1231220</link>	
		<description>You could explicitly create session variables from the form data, then manually populate the form from the session variables when the user goes back. That&apos;s probably the surest way to retain the data.&lt;br&gt;
&lt;br&gt;
As others have said, browsers tend to decide for themselves whether to retain form data automatically, but don&apos;t rely on it. A bit of jiggery-pokery with headers might help, but really you might be best to try to take control of the data via session vars.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.83127-1231220</guid>
		<pubDate>Fri, 08 Feb 2008 07:55:27 -0800</pubDate>
		<dc:creator>le morte de bea arthur</dc:creator>
	</item><item>
		<title>By: odinsdream</title>
		<link>http://ask.metafilter.com/83127/Retaining-form-data-when-going-back-and-still-using-PHP-sessions#1231423</link>	
		<description>Got it. It looks like I&apos;ll need to go about reworking the code a bit, then.&lt;br&gt;
&lt;br&gt;
When I say added sessions, I mean I added a login page that starts a session, then I also adjusted the other pages to require that a session has already started before they are displayed.&lt;br&gt;
&lt;br&gt;
Before this change, I could enter information on the form, go to the Submit step and view any errors, then go back and the data I had entered would remain in the form.&lt;br&gt;
&lt;br&gt;
After I created a login page and started restricting the pages to only requests within the same session, this behaviour changed and now results in the form always showing up empty when I go back to it from the submit page.&lt;br&gt;
&lt;br&gt;
I am not currently doing any step to fill in the form, but now I will have to adjust and store everything in session variables in order to repopulate in case of errors.&lt;br&gt;
&lt;br&gt;
If anyone has some nifty frameworks they use, I&apos;d love to see those.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.83127-1231423</guid>
		<pubDate>Fri, 08 Feb 2008 10:50:48 -0800</pubDate>
		<dc:creator>odinsdream</dc:creator>
	</item><item>
		<title>By: bertrandom</title>
		<link>http://ask.metafilter.com/83127/Retaining-form-data-when-going-back-and-still-using-PHP-sessions#1231654</link>	
		<description>I use &lt;a href=&quot;http://www.symfony-project.org&quot;&gt;symfony&lt;/a&gt; for a lot of bigger projects, it might be overkill for small ones though. It has really good form validation routines and can automatically fill the form values back in by parsing the XHTML, in the case of an error. The documentation is thorough and well written. &lt;a href=&quot;http://www.symfony-project.org/book/1_0/10-Forms&quot;&gt;This page&lt;/a&gt; on forms might give you a good example.&lt;br&gt;
&lt;br&gt;
If you&apos;d rather roll your own, I&apos;ve combined &lt;a href=&quot;http://smarty.php.net&quot;&gt;Smarty&lt;/a&gt;, a great templating engine with &lt;a href=&quot;http://pear.php.net/package/DB_DataObject&quot;&gt;DB_Dataobject&lt;/a&gt;, a database abstraction library, and thats worked out pretty well. Even using Smarty alone will save you a bunch of time, there are a lot of useful helpers, like dynamically creating SELECT boxes, although I&apos;m sure the PHP-is-a-templating-language people will disagree.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.83127-1231654</guid>
		<pubDate>Fri, 08 Feb 2008 13:36:11 -0800</pubDate>
		<dc:creator>bertrandom</dc:creator>
	</item>
	</channel>
</rss>
