Form Textbox Caching Problem - php

I have a PHP form, with various input fields and textboxes. If you submit and go back, all of the data that was submitted in the input fields remains, however the textboxes are blank. How can I get the data entered in the textbox to cache like the regular text inputs?

This is a client-side issue. Although there's no way to force the browser to cache the textarea input you can send the data back yourself using cookies if you want. One easy way to do so would be to store the textarea input in cookies when the form is submitted and then to check for the cookies and insert the values into the page source on subsequent requests to the server.
Check out this page for information on setting the cookies and this one to learn how to access the information the next time your form is accessed.

I generally prefer the back button myself, but if you want to re-populate all your fields, an alternative is to have the form page submit to self and then do like this:
<form action="whatever" method="POST">
<input type="text" size="20" name="text_field" value="<?php echo $_POST['text_field']; ?>">
<textarea name="text_area"><?php echo $_POST['text_area']; ?></textarea>
<input type="submit" value="Submit">
</form>

Related

How to POST form data from restored session?

I use Firefox add-on called "Session Manager" to save and restore sessions. I have simple php + html form:
<form id="form_id" enctype="multipart/form-data" method="post" action="upload.php">
<input id="name$key" type="text" placeholder="Name" name="name[]" value="$name">
<input type="file" name="fileToUpload[]" id="fileToUpload$key">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
When I restore form inputs data with "Session Manager" I can see all data I need. When I click the "Submit" button, data have empty $_POST.
What can I do to not lose this data?
Maybe to use some JQuery or session_start(); $_SESSION?
Firefox add-on "Session Manager" seems to work incorrect if html <form> setted with attribute enctype="multipart/form-data". If you want to send some files through POST use <form> attribute enctype="application/x-www-form-urlencoded" in conjunction with php copy(). That's not clean solution. Maybe there could be other solutions with enctype="multipart/form-data", maybe some expirements with form accept-charset could give you better results.
The Session Manager plugin in Firefox is not at all related to PHP sessions. Same word, entirely different meanings.
A Firefox session is your browser tabs and the websites they are accessing. A PHP session relates to a user session on a specific website.
Most likely the data you are seeing "saved" in the forms is just field data that is saved in Firefox only, for the sole purpose of making data re-entry faster. It is not yet actually "in" the form fields, but saved in Firefox (only, not on the website) in order to make easier the re-entry of frequently typed data.
When you lose a connection to a website, you lose the data typed in the fields. Refreshing the page loses the data typed in the fields. There is no work-around for this, it's just how it is.
If you have further questions, please ask in comments below this answer.
Edit:
Re-thinking, it may be possible to achieve some kind of solution using a javascript/jQuery (please, jQuery) solution that involves detected when fields are exited (blur()) and subsequent grabbing of the data and saving in localStorage.
References:
https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
http://www.w3schools.com/html/html5_webstorage.asp
When is localStorage cleared?
What is the max size of localStorage values?

Auto fill form data after autocomplete="off" and value=""

I have a simple log in form in php, and I have two forms each on in separated web-page.
when I fill the log in information and go to another form in another web-page, the new form is filled automatically with the same data of the log in form.
And when I go back to log in page after logging out, the credential fields are filled the the old data as well.
I tried value="" for each input and autocomplete="off" and the autocomplete wasn't disabled. and I don't want to cache the data on the forms.
How to prevent one form to use the data from another?
and how to prevent caching the data and autofilling?
have you tried to apply the autocomplete="off" on the form tag and not in the input tag?
<form action="" method="" autocomplete="off">
</form>
Have you tried:
autocomplete="nope"
(or any other random string value for autocomplete, as suggested by Mozilla)?
See: https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion

How to refill file input after form submited with errors

I have an
<input type="text" name="name">
<input type="file" name="photo">
After submit it form, I found some errors, so, I do:
<input type="text" name="name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<input type="file" name="photo" value="WHAT HERE?!">
As you can see, I don't know what I need to write on the value attr to refill the selected file form the user.
Can I do that?
Thanks!
Unfortunately, this cannot be done because it presents too many security risks. When a file is uploaded, the browser is not given the actual file path but rather creates a temporary file (with a path value of C:\fakepath) that it can send to the server on processing. So there is no value you can pass through the POST that you can put back into an input to choose a file.
An alternative would be to use AJAX for form validation, that way the user never leaves the page until the form is ready to submit.
Can I do that?
No, you cant. For security reasons, the value attribute is ignored for input elements
of type="file".
Possible workarounds:
Keep a reference to the submitted file (that is stored on disk) in a hidden input. Show a message to the user to inform that the file not need to be entered again, because you already have it from previous failed submits.
Submit your form via AJAX.
Validate as much as possible on the client side with javascript to avoid reloads. This will assume javascript is enabled.

Retaining the value in the <input type="file"> field when submit button is clicked

I have got a <input type="file" > field along with other text fields in a form, When i try to upload any files using browse button and then click submit button ,the value in the input type= "file" field disappears , I would like the browsed value to remain in the <input type="file" > field if errors are present in other fields , is there any way i can retain the value that is browsed and for it to remain in the <input type="file" > field when submit button is clicked ,
<form action="form.php" method="post" enctype= multipart/form-data>
<input type="file" value="$file" name="file"/>
<input type="text" value="$line" name="line">
<input type="submit" name="btnsubmit">
</form>
if($_POST['btnsubmit'])
{
$line =$_POST['line'];
$file =$_FILES['file'] ['name'];
if($line)
{
//do something
//conditions for file check here
}
else
//error
}
It is not possible to do this. Browser security prevents you from pre-populating the File input field, so that websites cannot steal private files of their will without the user authorizing it first (by clicking "Browse..." and choosing a file).
EDIT: It is not possible to do this natively - but you can attempt some CSS wizardry to display the previously chosen file name maybe beside the file input box to hint the user. If you want to try and be really cool, you can also attempt to overlay the browser's native file input text display area with another div that has the previous file name filled in it. But this will prevent clicking on the input area and so is user unfriendly. Too much work, little reward.
This not allowed to be set by any script for security purpose, implemented by browser vendors as file input as readonly.
There is one way to do this. Submit the form details for validation using an AJAX submission method so the user never really leaves the page or "submits" the form. That way you can validate the result server-side but the user still has all their values populated, including file inputs.
As mentioned, input[type=file] is readonly. By validating your input on the client side, you can prevent the submit to happen unless all fields are valid. ...and, in most cases, it provides a much better user experience!
Check out jquery.validation or some other validation plugin for your favourite framework, or write one yourself. Keep in mind that you should also validate on the server side.
By preventing the request, the file input will keep it's value until all fields are OK. If you need server side validation, you could also do this using ajax.

What's the easiest way to re-populate a form when the page is reloaded?

What's the easiest way to re-populate a form when the page is reloaded? Is there a script in jquery, php, or javascript/ajax that would achieve this?
If the submitted failed, I want the user to go back. My problem is all my CSS hovers and background-changes don't work via keyup.
If you mean reloaded before it is submitted you will need to use JavaScript to capture and store the values in a cookie, or to the server using Ajax, and then refill the form on page load.
If you mean after it is submitted then will need to use PHP to re-populate the form with the submitted form values:
<input type="text" name="somefield" value="<?php if (isset($_POST['somefield'])) echo htmlspecialchars($_POST['somefield'], ENT_HTML5, 'utf-8'); ?>">

Categories