In PHP how can I create a form that has upload button, but it also has submit button where I submit that text from file and some other data (from other inouts)? Can I have two button elements in one form, or should I split my form into two forms, where one form has upload button, and another submit button? Should I use jQuery to upload file, but after that how can I access those data on action php file? Please help. Thank you
-
Have a look at these links http://php.net/manual/en/features.file-upload.php ,http://stackoverflow.com/questions/547821/two-submit-buttons-in-one-form,http://blueimp.github.com/jQuery-File-Upload/ – Arun Unnikrishnan Jan 19 '13 at 17:49
3 Answers
No need for multiple forms. To upload a file use <input type="file" name="MyFile">
and add the following attribute to the form
element:enctype="multipart/form-data"
After submitting the form to the server, you'll get a $_FILES
super-global array (in addition to the $_POST array which will contain the rest of the fields), in which you'll find all the details of the uploaded file. When you submit the form the file is uploaded to a temporary location, and you need to move it to its constant dwelling using the move_uploaded_file()
function.

- 6,233
- 9
- 47
- 80
-
I only need to take data from file, not to upload file to server. Is this the same procedure, and do I need to implement some methos that destroys $_FILES array? – Duka Nikolau Jan 19 '13 at 18:26
-
Upload the file to the server, and there you can use `file_get_contents()` to read its content and save it to a DB or whatever you need. No need to destroy the array. It will be gone as soon as you redirect to another page after processing the form. Remember: the web is stateless – Matanya Jan 19 '13 at 18:30
Yes you can. You could do an upload script that is triggered via upload button. Include that script in the beginning of your form. Something along these lines:
<?php
if(isset($_POST['upload'])) {
//In your upload script you could store all the upload data in $_SESSION
include('yourUploadScript.php');
}
if(isset($_POST['submit'])) {
//Trim and escape post data here
//Send the post data and file upload data via your own submit function/script or whatever
}
?>
<html>
<body>
<form method="post" name="myForm" action="thisphp.php" enctype="multipart/form-data">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" name="upload" value="upload" />
First name: <input type="text" name="fname"><br />
Last name: <input type="text" name="lname"><br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
Remember that action should be this form/php file. Note also that this html, depending on your doctype, might not be valid. This is just to demonstrate.

- 425
- 1
- 5
- 18
-
After upload button, I should stay on that page, not thisForm.php, so I can press Submit button. How can I do that? – Duka Nikolau Jan 20 '13 at 10:24
-
Have a look at these links
http://php.net/manual/en/features.file-upload.php

- 1
- 1

- 2,339
- 2
- 25
- 39