Multiple selection of radiobutton

Asked

Viewed 177 times

1

I’m making a registration form that has the link Add more fields and when the person clicks on it, multiplies the form fields. One of these form fields is 2 radios Buttons, which are "Do you have children? ( ) Yes ( ) No" , and when you click to add more fields to the selection that is in ( ) Yes (by default) it disappears and goes to the field that was added; and if I click to select the top radio button, it leaves the bottom and goes to the top, so among the 4 radio Buttons I have in the form, I can only select 1. The name of the radiobuttons are like this name="children[]" but are not working.

jQuey code (multiplied)

<script>   $("a#adicionarMaisCampos").on('click', function(){

    var html = '<tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>Nome</td><td><input type="text" name="nome[]" class="form-control"></td></tr><tr><td>Data de aniversário:</td><td><input type="text" name="nascimento[]" class="form-control" placeholder="Somente o dia e mês. ex: 12/04"></td></tr><tr><td>Filhos:</td><td><input type="radio" name="filhos[]" value="1">Sim <input type="radio" name="filhos[]" value="2" checked> Não</td></tr><tr><td>Profissão:</td><td><input type="text" name="profissao[]" class="form-control"></td></tr><tr><td>Sexo</td><td><input type="radio" name="sexo[]" value="1" checked> Masculino<input type="radio" name="sexo[]" value="2"> Feminino</td></tr><tr><td>Email:</td><td><input type="email" name="email[]" class="form-control"></td></tr><tr><td>Celular:</td><td><input type="text" name="celular[]" class="form-control"></td></tr><tr><td>Cargo:</td><td><input type="text" name="cargo[]" class="form-control"></td></tr>';

   $("#more").after(html);


  });   </script>

HTML

<tr>
                      <td>Nome</td>
                      <td><input type="text" name="nome[]" class="form-control"></td>
                    </tr>

                    <tr>
                      <td>Data de aniversário:</td>
                      <td><input type="text" name="nascimento[]" class="form-control" placeholder="Somente o dia e mês. ex: 12/04"></td>
                    </tr>

                    <tr>
                      <td>Filhos:</td>
                      <td><input type="radio" name="filhos[]" value="1">Sim <input type="radio" name="filhos[]" value="2" checked> Não</td>
                    </tr>

                    <tr>
                      <td>Profissão:</td>
                      <td><input type="text" name="profissao[]" class="form-control"></td>
                    </tr>

                    <tr>
                      <td>Sexo</td>
                      <td>
                        <input type="radio" name="sexo[]" value="1" checked> Masculino <input type="radio" name="sexo[]" value="2"> Feminino
                      </td>
                    </tr>

                    <tr>
                      <td>Email:</td>
                      <td><input type="email" name="email[]" class="form-control"></td>
                    </tr>

                    <tr>
                      <td>Celular:</td>
                      <td><input type="text" name="celular[]" class="form-control"></td>
                    </tr>

                    <tr id="more">
                      <td>Cargo:</td>
                      <td><input type="text" name="cargo[]" class="form-control"></td>
                    </tr>

                    <tr>
                      <td>&nbsp;</td>
                      <td><a href="javascript:void(0);" id="adicionarMaisCampos"><i class="fa fa-plus"></i> Adicionar mais campos</a></td>
                    </tr>
  • The name of all radiobuttons are equal?

  • 1

    When multiplying the form fields, you must change the name of the two new Radiobuttons. If you can post the code that does this multiplication I can elaborate a more direct response.

  • @Vinícius yes, because in PHP I’m taking $_POST['sons'] and making a loop with foreach to catch everyone (if you have + 1)

  • @Vinicius added the code

  • HTML by default uses the name of inputs to perform match. By placing four radiobuttons with the same name, it will identify as if they were four options instead of just 2. I’m not a PHP developer, but I’m checking here how I can help you.

  • @Vinícius is what would make it easier for me, because if I put 2 equal names, then other 2 equal names but different from the other 2 and so on, it would be VERY laborious in PHP checking

  • But from what I understand Alisson, are really two attributes isn’t it? If there are two on the screen, in your backend there should also be two others, because the values may be different.

  • Initially will be 2 radios Buttons with the same name with the values yes or no. Only when the user clicks on add more it then adds +2 radios Buttons with the same name and the same value Yes or No. And in PHP he won’t know if he clicked on the link to add more unless I leave the same name (the way q is now) and make a loop

  • 1

    Friend, you must generate name different for all radio fields and before sending save in an array and sending these arrays to the server. I think it is the only way to do.

Show 4 more comments
No answers

Browser other questions tagged

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