keep file-upload value after page reload - php

I have a huge form that lets users (along with a lot of other inputs) upload multiple images, like this:
<input type="file" id="product_images" class="hidden" name="upload[]" multiple>
<input type="file" id="product_images_2" class="hidden" name="upload[]" multiple>
...
Now, when there is an error in a certain input (e.g. String for "description"-field too short), the page reloads. After this reload I need to keep all the posted data in all the inputs. For a text input I know how to do it, like this:
<input name="name" type="text" value="<?php echo $_POST['name']; ?>"/>
but, how can I achieve this for the file inputs?
I tried something like this:
<input type="file" id="product_images" name="upload[]" multiple value="<?php print($_POST['upload']); ?>">
Did not work though. Any ideas? Thanks!

Maybe that's because you are validating in server, try to validate your field in browser, if the server reloads it will loose all your data saved in fields.
Try this validator personally i like it:
http://jqueryvalidation.org/
Hop it helps

$_SESSION["image_path"] = $_POST['upload'];
<input type="file" id="product_images"<?php values= if(empty($_session['image_path'])) {} else { echo $_session['image_path']; } ?>class="hidden" name="upload[]">
after you retrieved ,Destroy the session

Try to use pattern in your input bar that will help you , you can do it very easily
http://www.wufoo.com/html5/attributes/10-pattern.html
http://www.w3schools.com/tags/att_input_pattern.asp

Related

Prefilled html file input?

So I have a very simple file upload form, code below...
<form enctype="multipart/form-data" action="$theaction" method="POST">
<input name="source" type="file">
<input name="message" type="text" value="">
<input type="submit" value="Upload"/>
</form>
I was wondering if I can set with php the source of the file upload? Something like..
<?php $file = "http://mywebsite.com/path/to/img.jpg"; ?>
<input name="source" type="file" src="<?php echo $file; ?>">
I've googled it but I can't come up with anything. I feel like I'm just not using the proper vocabulary, but any help would be greatly appreciated!
I concur #IMSoP statement.
Instead of populating the input tag value, I would rather create an element (div) to display the thumbnail version of the image that was uploaded by the user that way it makes it clear to the user that a file exist and also I would give them the option to edit for uploading a new file & delete for deleting the file.

Youtube API upload from website WITH diff. TITLES

I am using this code to make a upload-form for my website.
Youtubeuploader
It works perfect, but I got ONE problem.
I want a form to define the title.
Right now, all the videos are given the title "Example"
I want the user to write their own title for their video.
I've tried making a input with id and name "title", and tried to use $_POST['title'], but it didn't work. The video didn't even upload.
Anyone knows how to do this?
I've looked at all the YouTube Data API code and tried everything.
Thanks.
Given the link you posted, you have to give $youtube_video_title the value from your form.
I mean like this (not secure, always sanitize and filter):
$youtube_video_title = $_POST['title'];
UPDATE :
try to replace the form code with this one
<form action="<?php echo($response->url); ?>?nexturl=<?php echo(urlencode($nexturl)); ?>" method="post" enctype="multipart/form-data" onsubmit="return checkForFile();">
Title : <input id="title" type="text" name="title"/>
<input id="file" type="file" name="file"/>
<div id="errMsg" style="display:none;color:red">
You need to specify a file.
</div>
<input type="hidden" name="token" value="<?php echo($response->token); ?>"/>
<input type="submit" value="go" />
</form>

Form with multiple submit option. PHP

I have a HTML form page with following code :
<form action="chainresult.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="submit" value="GET SEQUENCE" />
</form>
<form action="helix_info.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="submit" value="GET HELIX INFO" />
</form>
My page has two browse options and two submit options which takes the use to 2 php pages. I want to have only one browse option with two options that takes the user to 2 different php pages based on what the user clicks.
Any help is appreciated!
You will need to combine the two forms into one (you don't even necessarily need the form tags), use JavaScript or jQuery to capture the submit button click, evaluate the input value based on your validation rules that route the form submission, and then post the values to a form, likely through ajax.
You can submit multiple forms but you will have to use Javascript. It should be doable with jQuery without too much sweat and tears. Something like...
$("#my-submit-button").click(function(){
$("#first-form").submit();
$("#second-form").submit();
})
I am not sure I understand your question correctly, but if you want to be able to post data to two different URLs, with two different submit-buttons, having two different forms is the only way to do it with plain HTML.
However, it would be possible to use JavaScript. In that case you could mash both forms together, evaluate the input before sending any information, and the post the data to different URLs depending on the input, using AJAX.
It is worth noting that going down the JavaScript-road, you would make the form unusable for anyone who has disable JavaScript.
I would probably suggest that you make it a single form, point it to an URL that can handle either case. So you always post the data to the same URL, and the server-side code would have to evaluate the input and decide what to do with it. That case you don't eliminate users that doesn't have JavaScript activated.
Not sure if I understood the question properly, but you could use jQuery to change the action attribute of your form, depending on what the user chooses. Something among the lines of:
<form id="myform" action="dummy.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="radio" name="formtype" value="uploadscript1.php" /> Option 1<br>
<input type="radio" name="formtype" value="uploadscript2.php" /> Option 2<br>
<input type="submit" value="GET HELIX INFO" />
</form>
And in jQuery:
$('input[name="formtype"]').change(function(){
$('#myform').attr('action', $(this).attr('value'));
});
I am not sure about the jQuery part, but it should work ok. Try experimenting with that. :)
Using this approach you should be able to send the same data to two different forms.

Get $_POST from type=file

I have form for example:
<input type="text" />
<input type="file" />
<input type="submit" />
You have to fill the text and the file, if you dont fill the text for example you have an error message, but I want to preserve what the user filled in the file's input.
How to do it with $_POST or somthing else?
I want to preserve what the user filled in the file's input.
Impossible.
NOTE:
$_POST is not the superglobal that will provide information related to 'file' inputs'. Look into you $_FILES superglobal for that kind of information.
You have to specify the "name" attribute of each input to something different:
<input type="text" name="title" />
<input type="file" name="picture"/>
<input type="submit" name="submit_button_1"/>
<input type="submit" name="submit_button_2"/>
Setting the name attribute of the submit input can be useful when you have several of them and you want to know on which one you clicked.
whatever user submits from client-side is not controlled by server-side, but you can:
use javascript to "scan" the extension name of file (.php), and prevent other files from not being able to be submitted. However this doesn't stop people renaming other files to .php.
the question is what you gonna do with the submitted php file? keep it in database? or execute it? you need to scan its content to make sure it doesn't contain malicious code. I am not very sure how php can do this, though.

How to save information in inputs inside a form so it doesn't disappear when form is submitted to itself?

I have a picture upload inside a form...
The file is a php file btw...
Problem is whenever this form is filled in, and the user clicks to upload the first picture, the form is submitted to itself and all the fields which the user may have filled in will go blank...
I know of one way to do it, alot of 'isset' in my php code, but is there any simpler or maybe better way I don't know of?
Thanks
You echo back the POST variable on your fields.
<form method="POST">
<input type="text" name="name" value="<?php echo $_POST['name']?>" />
<input type="submit" name="submit" />
</form>
When the form is submitted to self, the same data will be filled.
Well i do not know of anything else. I always use this:
<input type="text" value="<?= isset($value) ? $value : ""; ?>">
I think it is not too much code in the Templates, but it does the Trick.
Alternatively you could use some Frameworks wich abstract everything for you, but i cannot recommend some...

Categories