php - Storing Form element ID in variable?
585
I have an HTML form that contains the following code:
<input id="image_22" name="images[]" type="file" />
<input id="image_8" name="images[]" type="file" />
I'd like to be able to store the form input ID element in a variable (e.g. $imgnumber = 'image_22'). Is this possible? See my comment in the code below:
PHP:
for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
$number_of_file_fields++;
if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['images']['name'][$i];
if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
$number_of_moved_files++;
// *** COMMENT *** //
$imgnumber = CURRENT_FILE_INPUT_ID - this would store e.g. "image_22"
}
}
}
Many thanks for any help with this :-)
Answer
Solution:
Only the
name
and value of inputs are submitted to the server side. You could code the ID into the name instead of using an array of files.Access on the PHP side:
Edit:
Your updated PHP code could be:
Failing that, you will have to send the ID as a separate variable, for example using a hidden input or the querystring.
Answer
Solution:
You can't do it in pure HTML+PHP for the simple reason that browsers do not send IDs to the server when forms are submitted.
The most obvious solution is to add the relevant information somewhere else, such as the
name
attribute.Update:
Rather than:
... do this:
Update:
If you
print_r()
your input, you'll see what it looks like: