0

Good day! I'm a newbie to PHP and would like to ask some help.

I have a code for auto generating a question from database together with 5 checkbox. My problem is that, how can I save different value from the checkbox?

Any idea would be greatly appreciated!

<?php

        $sql = "SELECT * FROM question_pool_registrar WHERE criteria_id = 1";
        $result = $connect->query($sql);

        if($result->num_rows > 0){

            while($row = $result->fetch_assoc()){
            $question = $row['question'];
                echo "<tr>
                <td>$question</td>
                <td><input type='checkbox' value='1C-Q1-5' name='answer1' class='radio'></td>
                <td><input type='checkbox' value='1C-Q1-4' name='answer1' class='radio'></td>
                <td><input type='checkbox' value='1C-Q1-3' name='answer1' class='radio'></td>
                <td><input type='checkbox' value='1C-Q1-2' name='answer1' class='radio'></td>
                <td><input type='checkbox' value='1C-Q1-1' name='answer1' class='radio'></td>
                      </tr>";
            }
        }

        ?>

J_D
  • 740
  • 8
  • 17
  • you mean increment value ? – prasanth May 21 '19 at 06:22
  • no. the codes gets the question with criteria id 1 from dtbase. whenever i add a new question with criteria id 1, a new set of checkbox with the same name and value will appear. how can i save it to database if they have the same value – aldrian diaz May 21 '19 at 06:26

1 Answers1

0

The behavior of checkboxes is that, they would require to have the same name for their input tags but they will be treated as an array, so you need to add the ** [ ] ** braces in their names

<input type='checkbox' value='1C-Q1-5' name='answer1[]' class='radio'>

So now, depending if you'll use GET or POST methods:

$_POST['answer1'] //this is an array

$answers = $_POST['answer1'] 
// we pass the fetched data first
// then we transform then into a string if you want to save them in your database

$final_answers = implode("," , $answers);

//the $final_answers variable is a STRING that now holds all the chosen checkboxes but are separated by a comma
//for example, it will hold a value "1C-Q1-5, 1C-Q1-2, 1C-Q1-1", if the user has checked the 1st, 2nd and 5th checkboxes
Suomynona
  • 639
  • 1
  • 5
  • 20
  • additionally, if you're wondering why do we need to transform the checked checkboxes' data into a string, it's because the database doesn't accept an array as a value/data :) – Suomynona May 21 '19 at 06:30
  • 1
    Although you could change the design of the database so that it allows multiple rows for answers. This can make processing easier (in some cases) rather than having a comma separated list of answers - https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad – Nigel Ren May 21 '19 at 06:35
  • so, if i put the value of checkbox to an array, everytime i try to add a new question and auto generate 5 new checkboxes, they will have different value depending on whats inside the array? – aldrian diaz May 21 '19 at 06:35
  • yes yes @aldriandiaz, you're correct, and if you want to retrieve all the data that were saved in your database, do the reverse, you explode them to an array then traverse if you're going to display them back to the user ^_^ – Suomynona May 21 '19 at 06:37
  • hello sir @NigelRen, the problem is, he wanted a dynamic number of checkboxes, it'll be hard for him to assign a definitive number of rows just to satisfy the number of checkboxes. but indeed, your idea is brilliant – Suomynona May 21 '19 at 06:38
  • thank u so much for ur fast answer @JueViole17. i understand it now and will try what you've said. – aldrian diaz May 21 '19 at 06:45
  • if you have confirmed that my answer / suggestion works perfectly fine, feel free to comment and ask for more :) Also, if you may like, you may mark my answer as the official one by clicking the check button, you may also upvote it :D – Suomynona May 21 '19 at 06:47