0

I've a form with too many fields. One of these fields is as follows:

<form action="go_to_preview.php" role="form" method="post" enctype="multipart/form-data">
  <input type="file" name="equip_image" id="equip_image">
</form>

Now the purpose of file go_to_preview.php is to show the data filled in by user in the form of labels. All the filled in data are also contained in hidden fields. If user finds whatever he/she has filled in is perfect then user clicks the submit button present on file go_to_preview.php. As soon as user clicks on submit button I'll add the data I received through $_POST to the database. Now my issue is along with $_POST(which is an array of data present into hidden fields) I want the data from $_FILES as it is. How should I achieve this? Can someone please help me in this regard? Thanks in advance.

1
  • When you submit your form, both the $_POST and $_FILES data are sent as seperate arrays, you can see this by going to go_to_preview.php and using var_dump($_FILES) and var_dump($_POST), you should see both have the correct contents :) Commented Sep 9, 2014 at 19:51

2 Answers 2

3

A common approach would be sessions:

//go_to_preview.php
session_start();
$_SESSION['FILES'] = $_FILES;

//final.php
session_start();
$files_var = $_SESSION['FILES'];
//use $files_var
unset($_SESSION['FILES']);
Sign up to request clarification or add additional context in comments.

8 Comments

But after submission of a form for last time when I insert the data into database the $_FILES should get removed from sessions. How should I do it?
Edited: Use unset().
Thanks for your help but can you tell me if I don't use sessions and store the $_FILES data into a hidden field. Will it work?
Yes, it should be the same. I use session for both instead of hidden fields. Seems easier to say $_SESSION['fields'] = $_POST than to create a bunch of hidden fields.
I tried storing $_FILES array data into hidden field but I'm getting blank hidden field. why?
|
0

If I have understood your question correctly you are asking if you can pass both the $_FILES and $_POST arrays for processing in go_to_preview.php, the answer is yes.

The $_FILES array is populated when you selected an image via your equip_image element. Upon posting your form any specified POST data in that form will be sent in the headers to go_to_preview.php, the $_FILES array is sent too, this is seperate from your $_POST array.

In your go_to_preview.php you can then get the contents of both arrays:

$file = $_FILES['equip_image'];
$data = $_POST['form_data'];

If you wish to then unset that data you simply call

unset($_SESSION['FILES']);

2 Comments

They are then posting the values again to another "final" page and are able to pass the previous $_POST using hidden fields but need to also pass $_FILES.
Just seen that an saw your answer, my bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.