L
L
Lev Rozanov2020-04-15 19:29:15
PHP
Lev Rozanov, 2020-04-15 19:29:15

Array of nested (dependent) fields in $_POST?

I have a form with this structure:

<label>Вопрос №1</label>
<input type="text" name="???">
<label>1 ответ на 1 вопрос</label>
<input type="text" name="???">
<label>2 ответ на 1 вопрос</label>
<input type="text" name="???">
<label>3 ответ на 1 вопрос</label>
<input type="text" name="???">

<label>Вопрос №2</label>
<input type="text" name="???">
<label>1 ответ на 1 вопрос</label>
<input type="text" name="???">
<label>2 ответ на 1 вопрос</label>
<input type="text" name="???">


The number is different for each question.

I need to somehow do:
- write the value of 1 question to the database,
- write the value of 1 answer to 1 question to write to the database
- write the value of 1 answer to 1 question to the database
...
- write the value of 5 questions to the database,
- the value of 1 answer for question 5 write to the database
- the value of 2 answers to question 5 will be written to the database

I will write it like this:
- got the value of 1 question, wrote it to the table and got the id
- got the value of 1 answer to 1 question, wrote the answer and question id to another table
- got the value 2 answers for 1 question, wrote the answer and the question id into another table
...
- got the value 5 of the question, wrote it into the table and got the id
- received the value 1 of the answer to the 5th question, wrote the answer and the id of the question to another table
- received the value of 2 answers to the 5th question, wrote the answer and the id of the question to another table

I saw this construction:
<label>Вопрос №1</label>
<input type="text" name="question[][]">
<label>1 ответ на 1 вопрос</label>
<input type="text" name="answer[][]">
<label>2 ответ на 1 вопрос</label>
<input type="text" name="answer[][]">
<label>3 ответ на 1 вопрос</label>
<input type="text" name="answer[][]">

<label>Вопрос №2</label>
<input type="text" name="question[][]">
<label>1 ответ на 1 вопрос</label>
<input type="text" name="answer[][]">
<label>2 ответ на 1 вопрос</label>
<input type="text" name="answer[][]">


But I don't understand how it works and how it can be used.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
4
4sadly, 2020-04-15
@MetisKot

<label>Вопрос №1</label>
<input type="text" name="question[]">
<label>1 ответ на 1 вопрос</label>
<input type="text" name="answer[]">
<label>2 ответ на 1 вопрос</label>
<input type="text" name="answer[]">
<label>3 ответ на 1 вопрос</label>
<input type="text" name="answer[]">

Then just iterate over the arrays with answers, for example, name answer1[], answer2[]

N
nokimaro, 2020-04-15
@nokimaro

<label>Вопрос №1</label>
<input type="text" name="question[1]">
<label>1 ответ на 1 вопрос</label>
<input type="text" name="answer[1][1]">
<label>2 ответ на 1 вопрос</label>
<input type="text" name="answer[1][2]">
<label>3 ответ на 1 вопрос</label>
<input type="text" name="answer[1][3]">

<label>Вопрос №2</label>
<input type="text" name="question[2]">
<label>1 ответ на 1 вопрос</label>
<input type="text" name="answer[2][1]">
<label>2 ответ на 1 вопрос</label>
<input type="text" name="answer[2][2]">

In PHP, respectively, everything will be immediately decomposed and available as an array
$_POST['answer'][1][1]
$_POST['answer'][1][2]
итд

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question