I have a dynamic form that allows someone to add a row that has 2 input types. The first one can be an image or text field and the second one is always a text field. I need the first field to be tied with the second field in post. So I can add to a database field1A and field2A in the same row. The issue is that a file upload is in its own array so I can’t just loop through my text array and know that the image is tied to that text field.
<input type="text" name="field1[]">
If I loop through my field1[] array and get the index it will be different than my $_Files and I don’t know where in the array my files are.
Field1 | Field2 ----------------------- A text | text ----------------------- B file upload | text -----------------------
Advertisement
Answer
Your best bet, like @fubar said would be to set the keys while you are rendering the html :
<input type="text" name="field[0][0]"> <input type="file" name="field[1][0]"> <input type="text" name="field[0][1]"> <input type="text" name="field[1][1]"> <input type="text" name="field[0][2]"> <input type="file" name="field[1][2]">
Then you could loop in PHP like this :
foreach($_POST['field'][0] as $key => $value) { $field1 = $_POST['field'][0][$key]; $field2 = $_POST['field'][1][$key] ?? $_FILE['field'][1][$key]; }