I have got a simple form which has few fields like name, address, etc and two fields for uploading images and finally two buttons, one for preview and the other is a final submission.
I want to make the form much more user friendly, so that when the user clicks the preview button, I show a small prview and still if the user has to makes some changes in the fields which he has already entered, I want to the show the form echoing all the values which the user has entered.
In case of text field I am just using this piece of PHP code inside the tag to echo the name
<input type="text" name="name" value="<?php echo (isset($_REQUEST['name']) ? $_REQUEST['name'] : ""); ?>" />
and so for other fields in the form.
My question is that how can I echo/show the uploaded file names even after clicking the preview button, which in my case does upload the two images into the specified directory, that is I can see that the uploaded images are stored in the specified directory. In case if the user wants to upload only one image, then he can see the names of the already uploaded images in the output form, so that the user doesnot need to choose both the fields for uploading the images one more time.
I hope I explained it in a understandable way, in case if something is unclear then please let me know.
P.S. A small example would be good to follow.
Regards
Maks
Unfortunately if you're using the file input type you cannot modify their values in any way. This is done for the safety of the user so that you don't specify a default file to upload from the user's system (you could theoretically upload a sensitive file if they didn't change the field from the default).
You could try using a text input field that's read-only and shows the url to the file the user uploaded. An edit button next to this field could create a file upload field and allow them to upload a new file.
Related
I want my previously uploaded file to be automatically selected while I am editing details which also include file upload.
I have used value attribute as in other types. But this doesn't work.
<input type="file" name="file" value="<?php echo $news['image']; ?> ">
I have to select the required file again while I am editing details. No file is selected as default while editing. I want to edit other details but not the file upload.
What can I do so that I do not have to select it again?
You can display image using
<img src="path/<?php echo $news['image']; ?>">
or if it is a file use the
My File
tag to link.
When you update the DB make sure you update the field image only when the new file is selected.
"I have to select the required file again while I am editing details"
...no you don't. Just write the server-side code so that if no new file is selected, it doesn't delete the existing one when the form is submitted. And you can provide a link to the existing file on the edit form so that the user can see what's already there.
P.S. You can't make it select the same file again in the input box because
a) that's a security problem, your code cannot select files from the user's device - otherwise malicious websites could use hidden file inputs to secretly steal files from a device. The browser enforces that the user must manually select the file, in order to prevent that possibility
and
b) it wouldn't be logical to try and select the same file again anyway - remember that an input file takes files from the local device, not the server. But there's no guarantee that the file still exists in the same location (and your server won't know where that is anyway), and hasn't been moved, renamed or deleted, or that the form is even being used on the same device as previously. And even if you ignore all that, what would be the point of re-uploading a file you've already got on the server? It would just be a waste of bandwidth.
Here is a situation,in which there are two page one for uploading image and other for displaying the image. on uploading image ,a choose file button is there and on display page ,the uploading image will be shown and a button is also for redirect to the first page ,now i want that when user click on display page (redirect button) then uploaded image in the database will automatically refill in the first page (choose file button).I am trying to feteh the image name and refill in the choose file button but the choose file button shows "No File Selected",how to do that
You cannot 'pre-populate' a FILE input value. (Its a browser security risk)
Its a good practice, to replace file upload inputs with image, and put there delete button. After that, show new, empty file input.
You cant use server path on file input.
Browser's prevent you from doing this, so that files cannot be uploaded without the users interaction.
I am using an input file type inside a form:
<input name="upfile" id="upfile" type="file" >
through which I am uploading images to a MySQL BLOB field of STUDENTS table. So far so good. With another php file I am retrieving and presenting in a form, data from STUDENTS and the user has the possibility of directly changing some of this data and sending it back to STUDENTS.
In case the user uploads (client side) a new image inside "upfile" , I can manipulate it (server side) with $_FILES array. In case the user removes(client side) the existing image, $_FILES has no content at all. However, the same happens in case the user does nothing with the existing image; $_FILES has no content again.
My problem is I cannot find a way to distinguish among erasing the existing image and doing nothing with it, since in both cases $_FILES has no content. So, how the server will know if the user preserves or deletes the existing image as to act correctly?
Thank you very much
How does the user know if they are deleting or not-changing the image? Give them a "delete image" checkbox or something so that they, and you, will know what should happen.
I'm Just working on a simple form in PHP but i have a problem, i have many fields in my form and one of these is a file input type, if there are no issues with the file uploaded but an error occurred on another field, How can I ( in the refreshed page) maintain the file previously uploaded?
Edit: We understood what you want, but this is not possible, filling the file-input again automatically after validation. The user has to select his files again, this is how browsers works, for security matters.
Well, since you know what you're doing, you dont really need a script, you really need some help/ideas on how to approach what you want. What I would do is:
- Save the path of the uploaded file in session user data variable, like $_SESSION['uploaded_file'], and retrieve it after error.
- Use AJAX, different form, to upload the file before user hit submit.
Obs: If the user gives up on completing the form even though he uploaded a file, his files will be on your server, 'stealing' some useful space. So I don't know if this is a really good approach, but a good one, would be using different forms, disassembling the upload action from the form's informations.
Real example: If user is filling out a form to be a member of your website/forum/whatever, just let him upload the profile picture later, only when his account is really active. You could be saving some trouble.
Another one: If the user just wants to change profile picture, also make the uploading action to have no influence (and vice-versa) with his profile informations and etc. This is a trouble-saver, and a better approaching solution.
But, if you're like uploading a photo to a gallery and setting description and etc for the picture, then you could be using of a more elaborated validation, using javascript/ajax to check if fields are being filled correctly before submiting data.
On your upload code first save the values into SESSION like:
$_SESSION[name] = $_POST[name];
then print it into your INPUTS like:
<input type="text" name="name" value="<?=$_SESSION[name]?>">
Don't forget to use session_start().
Or look for javascript alternatives
I'm creating an admin space for a local news company. The structure of an uploaded news is the following:
title
date
picture
text
They asked me to let them change the uploaded picture from the admin space, in case they uploaded a wrong image.
Therefore when a user clicks "EDIT EXISTING NEWS", the page displays the above mentioned parts (title, date, picture and text) in the appropriate input (text, textarea) and shows also the picture.
I created a little RECYCLE BIN icon under the picture, which if clicked, does the following:
using AJAX replaces the displayed picture with a FILE INPUT.
However this newly created input type="file" name="picture" isn't viewable, parseable, processable after SUBMIT. Simply I can't view the $_POST["picture"], telling me it isn't set.
What can I do to be able to see it?
Please help!
Many thanks!
Huba
Data uploaded with <input type="file" ... /> is not accessible through $_POST, but through $_FILES (even though it's received through a POST request). See handling file uploads.