RoBiNp Posted July 31, 2009 Share Posted July 31, 2009 Hello, Usually, after entering text etc in form inputs (normal html), these data are retained after reloading the page. But, I have a complicated (i.e. I can't just copy everything to here) php script that generates page where these data disappear after reloads. I copied the html source and saved it as a normal html file, and it works correctly (= data are retained). So there must be some obscure reason I'd like to know why this doesn't work (session? cookies? cache? ...?). Thank you, Robin Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 31, 2009 Share Posted July 31, 2009 You have to make sure you are setting the proper attributes for controls so that a value is assigned to them. The attributes can be any one of (and it depends on which type of input you are using): value, checked, selected Quote Link to comment Share on other sites More sharing options...
bundyxc Posted July 31, 2009 Share Posted July 31, 2009 Well, how are you storing the in the page that echoes results? If you're using a $_POST, then it isn't going to save your variable unless you resend the data. Try changing your variables to $_GET and see if that helps at all. Quote Link to comment Share on other sites More sharing options...
Mardoxx Posted July 31, 2009 Share Posted July 31, 2009 bundyxc: I think he means when he does F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5 so there will be no posting of data ---- I think, due to security (and probably memory)... browsers don't remember data on forms.. eg.. type in a search into google... but dont submit it..press F5 (or ctrl+r w/e) - it doesn't keep it. unless I'm not talking about what you're asking... then just ignore me Quote Link to comment Share on other sites More sharing options...
bundyxc Posted July 31, 2009 Share Posted July 31, 2009 Oh.. DUH. Forget I said anything.. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 31, 2009 Share Posted July 31, 2009 When a form is submitted, if you want the form to be displayed again with the submitted data - YOU need to populate the data. It is not some magical server setting. Here's a very generic example: <?php $uname = (isset($_POST['uname'])) ? $_POST['uname'] : ''; if (!empty($uname)) { //Do something with the posted value $response = "<div>You submited the name: $uname</div>"; } else { $response = ''; } ?> <html> <body> <?php echo $response; ?> <form method="POST"> Enter your name: <input type="text" name="uname" value="<?php echo $uname; ?>" /> <button type="submit">Submit</button> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
RoBiNp Posted August 1, 2009 Author Share Posted August 1, 2009 I guess nobody understood me correctly, sorry. But, for example, the most simple form in plain html: <form method="post" action="post.php"> <input type="text" name="example" /> <input type="submit" /> </form> If you enter something, then reload the page (not submitting), the text you entered is still there. But it isn't in my PHP script (in short, it calls a custom class that stores member vars of class Template and at the end Template outputs an html page with those vars). Same with form submit: normally when hitting "back" in your brower, the text you entered is still there but it isn't in my PHP script. Really strange, I hope someone can find an explanation (otherwise, I'll have to deconstruct all my PHP scripts until it behaves normally ). Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 1, 2009 Share Posted August 1, 2009 I dn't think you are going to be able to control this 100% as it is a function of the browser and the user's settings. You need to look into adding headers to your page to instruct the browser to cache the data. But, again it is up to the browser to determine if it will adhere to those settings or not. For example, using a simple for such as you have above, when using the back key in IE or FF the data is redisplayed. However, if I refresh the page with data in the field, the data is wiped out in IE, but not in FF. But, of course, this behavior will be affected by setting in my browsers. I can also influence that behavior with using specific headers. For example this header: header("Cache-Control: no-cache, must-revalidate"); will prevent the data from repopulating the filed when using the back button, but only in IE. To be honest, I don't really understand all the different cache headers and their values, but that is probably where you need to be looking. Quote Link to comment Share on other sites More sharing options...
RoBiNp Posted August 1, 2009 Author Share Posted August 1, 2009 I dn't think you are going to be able to control this 100% as it is a function of the browser and the user's settings. You need to look into adding headers to your page to instruct the browser to cache the data. But, again it is up to the browser to determine if it will adhere to those settings or not. For example, using a simple for such as you have above, when using the back key in IE or FF the data is redisplayed. However, if I refresh the page with data in the field, the data is wiped out in IE, but not in FF. But, of course, this behavior will be affected by setting in my browsers. I can also influence that behavior with using specific headers. For example this header: header("Cache-Control: no-cache, must-revalidate"); will prevent the data from repopulating the filed when using the back button, but only in IE. To be honest, I don't really understand all the different cache headers and their values, but that is probably where you need to be looking. I don't mind it being different in browsers. It's just strange that it's different with the same browser settings, so the problem should be with PHP. I tried changing cache-control, but I don't understand it either.. Perhaps I will go through whole my php script to find the problem... Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 2, 2009 Share Posted August 2, 2009 It is not a problem with your script. The difference you are seeing is based on accessing a page locally vs. via a web browser. If you take the simple example page you have above and make it a local HTML file and as a PHP page accessed from a web server you will see the same results you stated before. A page refresh will not empty the contents of the form for a local page, btu on a page accessed via a web browser they will be emptied. Quote Link to comment Share on other sites More sharing options...
RoBiNp Posted August 2, 2009 Author Share Posted August 2, 2009 When changing the PHP header() function, the header remained the same no matter what I changed. But, I put the header() function somewhere else, and now it did change.. Strange that the position matters, but at least it works now with header('Cache-Control: public, max-age'); Quote Link to comment Share on other sites More sharing options...
Mardoxx Posted August 2, 2009 Share Posted August 2, 2009 can't you add a header: Expires: Tues, 31 Dec 2020 23:59:59 GMT or something? also max-age needs a value afaik Quote Link to comment Share on other sites More sharing options...
RoBiNp Posted August 2, 2009 Author Share Posted August 2, 2009 Yes, I have set 'Expires' and several other headers as well. max-age needs a value indeed, but apparently without a value it works also (in Firefox at least). Anyway, my initial problem is fixed now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.