Jump to content

Text entered in form inputs disappear after page reload


RoBiNp

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

bundyxc: I think he means when he does F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5F5 :P

 

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 :P

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 :P).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'); :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.