0

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
    -----------------------
3
  • Can you pls add the complete sample code? Commented Jan 24, 2020 at 1:02
  • Show us how you add your dynamic fields in the form, and how you tried to process them, for us to better assist you in your task. Commented Jan 24, 2020 at 1:21
  • 1
    You can name your input fields whatever you want. Add an index to associate the fields together. E.g. field[0][text], field[0][file], field[1][text], field[1][file], etc. If you include more of your code, we can advise how you can change it to accomplish this. Commented Jan 24, 2020 at 1:38

1 Answer 1

2

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];
}
Sign up to request clarification or add additional context in comments.

Comments

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.