How to save several radiobuttons in Mysql database?

Asked

Viewed 508 times

0

Guys, I need to save several groups of radiobuttons in the bank. Example:

<input type="radio" value="sim" name="grupo1">
<input type="radio" value="nao" name="grupo1">

<input type="radio" value="sim" name="grupo2">
<input type="radio" value="nao" name="grupo2">

<input type="radio" value="sim" name="grupo3">
<input type="radio" value="nao" name="grupo3">

Are 3 different groups need to save the chosen radio value of each group in the bank.

Some help?

  • Guys, I forgot to tell you. Radio groups are dynamic depending on the page loaded. Depending can appear 3 groups, 4 groups, 120 groups. That’s the problem. I have to get the value of each group that appears when I load the page.

  • Solved. I created a for() to list all radios and capture values.

2 answers

0

Well, I’m not sure I understand your question yet here it goes..

You can for example in the "value" of each "radio", put 0 or 1, and consider 0 as no and 1 as yes. From there it is playing with these 2 values when the value is 1 marks when it is 0 ignores. For each group you use a unique ID genre.

Mysql example:

***************
page_id ------ group_id ------ radio
***************
page1 --------- group1 --------- 0
page1 --------- group2 --------- 1
page2 --------- group3 --------- 0
page2 --------- group4 --------- 0

You can use the same table on multiple pages with multiple groups, when you select to load use "WHERE page_id == page you want".

  • Thanks for the comment. Really it works. I added a comment to my question. I forgot to cocoar a remark.

  • Even if you have 120 groups, it’s still the same, as long as each group has a unique ID... You can also add a field in mysql to delimit the page. I will edit the above example.

0


Thiago When you submit the form the amount sent is only that of the radio selected. That’s because the radio group has the same name

You can test like this:

<form action="">
    <p>Pergunta 01</p>
    <label for="">Sim</label><input type="radio" value="sim" name="grupo1" required="">
    <label for="">Não</label><input type="radio" value="nao" name="grupo1" required="">
    <br><br>
    <p>Pergunta 02</p>
    <label for="">Sim</label><input type="radio" value="sim" name="grupo2" required="">
    <label for="">Não</label><input type="radio" value="nao" name="grupo2" required="">
    <br><br>
    <p>Pergunta 03</p>
    <label for="">Sim</label><input type="radio" value="sim" name="grupo3" required="">
    <label for="">Não</label><input type="radio" value="nao" name="grupo3" required="">

    <button>Enviar</button>
</form>
<?php 
    if ($_GET){
        echo "<p>Pergunta 01: </p> " . $_GET['grupo1'];
        echo "<p>Pergunta 02: </p> " . $_GET['grupo2'];
        echo "<p>Pergunta 03: </p> " . $_GET['grupo3'];
    }
?>
  • Thanks for the comment. Really it works. I added a comment to my question. I forgot to cocoar a remark.

Browser other questions tagged

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