Mark only one radiobutton for each line

Asked

Viewed 108 times

1

I have a list, where by line I will mark the radio button stating: HAS, DOES NOT, BROKEN.

How do I do and what is the correct way to send this to BD Mysql for later on the edit screen capture?

Remembering that these <tr> will be in a php foreach for listings

How to send to the database to then reuse on the edit screen.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<div class="row">
  <table>
    <thead>
      <tr>
        <th>NOME</th>
        <th>TEM</th>
        <th>NÃO TEM</th>
        <th>AVARIADO</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td>Documento</td>
        <td><label><input name="group[]" type="radio" checked /><span></span></label></td>
        <td><label><input name="group[]" type="radio" /><span></span></label></td>
        <td><label><input name="group[]" type="radio" /><span></span></label></td>
      </tr>
      <tr>
        <td>Manual</td>
        <td><label><input name="group[]" type="radio" checked /><span></span></label></td>
        <td><label><input name="group[]" type="radio" /><span></span></label></td>
        <td><label><input name="group[]" type="radio" /><span></span></label></td>
      </tr>
      <tr>
        <td>Vidro</td>
        <td><label><input name="group[]" type="radio" checked /><span></span></label></td>
        <td><label><input name="group[]" type="radio" /><span></span></label></td>
        <td><label><input name="group[]" type="radio" /><span></span></label></td>
      </tr>

    </tbody>
  </table>
</div>

  • 1

    It’s not missing to put this in a <form> to be sent after one click on a button for example?

  • Hello Thiago, here I have the <form>, but in this post makes no difference. Note that I can only score 1 in total radio. Where the right would be to score 1 per line.

  • This isn’t about the name=group[]?

  • Not because I have to send everything to the same comic book table

  • If you have the same name you are in the same group.

  • But you must capture separately. Look here: https://codepen.io/anon/pen/OYgajB

Show 1 more comment

2 answers

1

I’m not sure I understand but the property name indicates to which group the <input::radiobutton> belonging.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<div class="row">
  <table>
    <thead>
      <tr>
        <th>NOME</th>
        <th>TEM</th>
        <th>NÃO TEM</th>
        <th>AVARIADO</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td>Documento</td>
        <td><label><input name="group1" type="radio" checked /><span></span></label></td>
        <td><label><input name="group1" type="radio" /><span></span></label></td>
        <td><label><input name="group1" type="radio" /><span></span></label></td>
      </tr>
      <tr>
        <td>Manual</td>
        <td><label><input name="group2" type="radio" checked /><span></span></label></td>
        <td><label><input name="group2" type="radio" /><span></span></label></td>
        <td><label><input name="group2" type="radio" /><span></span></label></td>
      </tr>
      <tr>
        <td>Vidro</td>
        <td><label><input name="group3" type="radio" checked /><span></span></label></td>
        <td><label><input name="group3" type="radio" /><span></span></label></td>
        <td><label><input name="group3" type="radio" /><span></span></label></td>
      </tr>

    </tbody>
  </table>
</div>

  • Hello Augusto, thank you. And to send to Mysql, should I send to the same table as array? How should I do this sending part to the database?

  • That had to be in one <form> to submit a HTTP GET or HTTP POST, or be sent using an object XMLHttpRequest by means of javascript code.

  • Here it is inside the form.

  • Put in the back end code, or I can’t help you.

  • I made the update by putting this point.

  • In general it is the attribute action of <form> it has to be like this in case the back end is php,<form action='/minhapagina.php'>

Show 1 more comment

1

About why Radio doesn’t work, that’s what Augusto already said. You cannot use the same name for all buttons and there is no need for the name to be an array in your displayed code, as only a single value will be linked to each tag name.

On how to save to the bank, you first need a form with an action and a method, preferably this action by going to another page . php

<form method="POST" action="salvaNoBanco.php">
 .
 .
 .
     // Botões de Radio
 .
 .
 .
    <input type="submit" value="Confirmar">
</form>

Now on my example page (salvaNoBanco.php), you need to receive these values from Radio. Assuming the names are document, manual and glass, you save on the bench as follows:

$query = "INSERT INTO MinhaTabela(documento, manual, vidro)
VALUES ('{$_POST['documento']}', '{$_POST['manual']}', '{$_POST['vidro']}')";

if ($conexao->query($query) === TRUE) {
     echo "Salvo com sucesso!";
} else {
     echo "Error: " . $query. "<br>" . $conexao->error;
}

Browser other questions tagged

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