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

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

Related

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.

Preserving data over multiple form submission

I am building a web service in which a user can login to a backend, submit a form and then other users will be able to see this information on the front-end of the web service.
The data is submitted to the PostgreSQL database fine BUT on the second submission of the form (users can go back to the form and update their information if neccessary) if there is a blank field in the form, the existing data in the form is overwritten with blank data.
How can I avoid this? I think it would be good to populate the form fields with data from the database and allow the user to edit the form like that but I am unsure how to implement this using PHP. At the moment a user would have to fill out the form in full over and over to prevent this kind of data loss.
Could someone give me an idea or point me in the right direction?
pre-fill the form fields with earlier entries from the DB
<input type="text" value="<?php echo $fname;?>" name="first_name" id="first_name" />

Form 2 sql gets fields from Form 1

I have two forms form 1 and form 2.
The form 1 has 20 fields of data and it's action is to update.
In form 2 I have another action, but I want to get the 20 fields of data from form 1.
Is there any possible way to do this and how? Also form 1 and form 2 are in the same page.
I have already a solution which is putting 20 hidden fields in form 2 so form two can
get it from there which is tedious. But can I ask you guys is there a much easier idea?
You can save all the input from form 1 in a session variable and restore it in form 2. For that purpose, write a function which stores all $_GET and $_POST variables in an element of the $_SESSION array (using the parameter name as the key).
The "problem" is something that ever PHP developer has at some point solved.
The best way to do this would be to save data from previous form in $_SESSION. But this will be a valid solution, if you are filling out the form, while in the same session.
If you are building something like a e-commerce site, where user has to fill out several payment forms, or some large test-application, where students fill out multiform tests, it might be better to store data from each form in DB and associate it to UserId. This way the user gains ability to continue filling out the form from different location.
P.S. If you are using AJAX instead, you will mostly just add another layer of complexity on top of existing application. Especially if your skills in JavaScript are limited.
I think your solution isn't bad, simple and Swift !
But the only better way, is ajax.
In ajax you are not limited to a form, you can get every fields from every forms and then send them manually.
If you are not familier with ajax, and you want to use your non-ajax solution, you can use one form and put all fields of two last forms in this new single form, and use one action file to response to this form.In server side you can check the value of a field which is unique.
client side :
<form action="test.php">
<!-- fields of form 1 -->
<input type="text" name="text1"/>
<input type="text" name="text2"/>
.
.
.
<input type="text" name="text20"/>
<!-- fields of form 2 -->
<input type="text" name="text21"/>
<input type="text" name="text22"/>
.
.
.
<input type="text" name="text40"/>
<input type="submit" value="send"/>
</form>
server side:
<?php
if ( isset($_GET["text1"]) )
//do related jobs to form 1 here, and you can use all of filled fields
else
//do related jobs to form 2 here, and you can use all of filled fields
?>

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.

Form Textbox Caching Problem

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>

Categories