Reputation: 5599
I have a form, it passes the following values:
image_title,
image_description,
image_file
I wanted to present this form multiple times, so I did the following:
image_title_1,
image_description_1,
image_file_1
image_title_2,
image_description_2,
image_file_2
Multiple times, so I have the fields 1 - 10. I submit the form and print out the contents of the POST array, the only problem is any "image_title_#" after "image_title_1" doesn't exist in the array: but everything else does.
So the array would look something like:
image_title_1 -> "title!"
image_description_1 -> "description!"
image_file_1 -> file
image_description_2 -> "description!"
image_file_2 -> file
image_description_3 -> "description!"
image_file_3 -> file
So to work out what it is I swapped description and title with each other, however title still doesn't display for after 1. I'm not doing ANY processing, I'm literally just printing out the $_POST array before even touching it. This makes no sense, what could be causing it?
To clarify: The problem is "image_title_#" (example: image_title_3) doesn't get passed except for image_title_1, even if I re-arrange the order. I do no processing before outputting.
Edit, the html source is just:
<form method="post" action="">
<input type="text" name="image_title_1"></input>
<input type="text" name="image_description_1"></input>
<input type="text" name="image_file_1"></input>
<input type="text" name="image_title_2"></input>
<input type="text" name="image_description_2"></input>
<input type="text" name="image_file_2"></input>
<input type="text" name="image_title_3"></input>
<input type="text" name="image_description_3"></input>
<input type="text" name="image_file_3"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
Upvotes: 0
Views: 959
Reputation: 382909
A better solution would be converting them to array, try this instead:
<form method="post" action="">
<input type="text" name="image_title[]"></input>
<input type="text" name="image_description[]"></input>
<input type="text" name="image_file[]"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
Now, in your PHP script, you can get their array like this:
print_r($_POST['image_title']);
print_r($_POST['image_description']);
print_r($_POST['image_file']);
.
Suffixing field name with []
converts it to array. The other good thing here is that it has shortened your code too.
Once you have the array, you can loop through them using foreach
:
foreach($_POST['image_title'] as $key => $value)
{
// process them any way you want
}
Upvotes: 2
Reputation: 19325
The code works. I just cut and paste your form and do a test submit
Array
(
[image_title_1] => 1
[image_description_1] => 2
[image_file_1] => 3
[image_title_2] => 4
[image_description_2] => 5
[image_file_2] => 6
[image_title_3] => 7
[image_description_3] => 8
[image_file_3] => 9
[submit] => submit
)
Upvotes: 0