A
A
Alexey2015-10-20 16:07:43
PHP
Alexey, 2015-10-20 16:07:43

How to group form elements?

There is a form like this:

<form action="" method="post">
  <input type="text" name="group[]" placeholder="группа">
  <input type="text" name="text[]" placeholder="текст">

  <input type="text" name="group[]" placeholder="группа">
  <input type="text" name="text[]"  placeholder="текст">
  <input type="text" name="text[]"  placeholder="текст">
  
  <input type="submit" value="Отправить">
</form>

After submitting the form to the server, I need to make sure that the elements that come after the name="group[]" field are in a separate group of texts, so that they can then be displayed on the site for these same groups. The number of groups and elements in each group is not known, since the user adds them himself.
In other words, you need a result on the server, something like:
array(2) {
["group"]=>
  array(1) {
    ['text']=>
    string(14) "текст первой группы"
  }
  ["group"]=>
  array(2) {
    ['text']=>
    string(36) "текст второй группы"
    ['text']=>
    string(36) "текст второй группы"
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
petyagrill, 2015-10-20
@petyagrill

Do it this way

<form action="" method="post">
  <input type="text" name="group[0]" placeholder="группа">
  <input type="text" name="group[0]['text'][]" placeholder="текст">

  <input type="text" name="group[1]['text'][]" placeholder="группа">
  <input type="text" name="group[1]['text'][]"  placeholder="текст">
  <input type="text" name="group[1]['text'][]"  placeholder="текст">
  
  <input type="submit"  value="Отправить">
</form>

A
Alexey Skobkin, 2015-10-20
@skobkin

It's not very clear what you want.
But try putting each group in its own element <div>.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question