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

0

My final course project, in a very brief way, is a question and answer game, in this project, I have the "Alternatives" table, when I am going to ask questions, there are four fields with the name of alternatives as follows :

                                                                                                     

All four fields are this way I send this data to a controller, the question is: How should I receive the data in this driver? Should I hold a foreach? Use Javascript in the form? Or should I change the name of each field and receive it separately?

Thanks for the help.

    
asked by anonymous 24.05.2018 / 21:54

1 answer

0
  

When you put a "name" with [] brackets it is sent in array form 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:

  • Expresses 3 expressions at the beginning of the for         
    • Being the 1st expression evaluated only once . Usually 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. It can be considered the conditional of the for.
    •             
    • 3rd Expression is evaluated at the end of each iteration, usually used to change the value of the control variable.
    •         
  •     
  • Separate expressions using the; (semicolon).
  •     
  • Looping runs as long as the condition of the 2nd expression is true (True)
  •     
  • When the condition of the 2nd expression evaluates to false (False), the processing of the routine is diverted out of the loop
  •     
  • The looping code block must be enclosed by braces {}
  •     
  • End the code referring to the block in 4 spaces for readability, how to configure your text editor to transform Tabs into spaces
  •     
  • We use a counter for looping not to be an infinite looping, this is declared in the 1st expression
  • Do not forget to change the value of the counter in the 3rd expression so that we do not fall into an infinite looping.
24.05.2018 / 22:44