How to receive data from a form that has repeated field names?

Asked

Viewed 60 times

-1

My final project of course, in a well summarized way is a game of questions and answers, in this project, I have the table "Alternatives", when I will perform the registration of questions, there are four fields with the name of alternatives as follows:

inserir a descrição da imagem aqui

All four camps are this way Sending this data to a controller, the question is: How should I receive the data on this controller? Should I perform a foreach? Javascript usage in the form? Or I should change the name of each field and receive it separately?

I appreciate the help.

1 answer

0


When you put a "name" with [] square brackets it is sent as an array to the receiver.

//o array
$alternativa = $_POST['alternativa'];
//Retorna o número de elementos da array:
$result = count($alternativa);

for ($i = 0; $i < ($result) ; $i++) {
  echo " Alternativa " . $alternativa[$i] . "<br>";
}

Regarding the control structure for:

  • Declares 3 expressions at the start of the
    • The first expression being evaluated only once. Normally used to create the control variable.
    • 2nd expression is evaluated at the beginning of each iteration, if this expression returns false the looping is terminated. May be considered the condition of the.
    • 3rd Expression is evaluated at the end of each iteration, normally used to change the value of the control variable.
  • Separate the expressions using the ;(point and comma).
  • The looping is executed while the 2nd expression condition is true (True)
  • When the 2nd expression condition is evaluated as false (False) routine processing is diverted out of the looping
  • The block of code referring to the looping should be delimited by keys {}
  • Endente the code referring to the block in 4 spaces for legibility reasons, how to configure your text editor to transform Tabs into spaces
  • We use a counter so the looping does not remain an infinite looping, this declared in the 1st expression
  • Don’t forget to change the value of the counter, in the 3rd expression, so we don’t fall into an infinite looping.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.