Link to home
Create AccountLog in
Avatar of ichiragpatel
ichiragpatel

asked on

how do i retrieve checkbox values from database using php

<td class="auto-style1" style="width: 69px">

              <LABEL> <INPUT TYPE="checkbox" NAME="options0[]" value =" Yes " style="width: 20px" > Yes</LABEL><br/>
              <LABEL> <INPUT TYPE="checkbox" NAME="options0[]" value =" No " style="width: 20px" > No&nbsp; </LABEL>
              </td>
              <td class="auto-style1">
              <LABEL> <INPUT TYPE="checkbox" NAME="options1[]" value = "Yes" style="width: 20px" > Yes</LABEL><br/>
              <LABEL> <INPUT TYPE="checkbox" NAME="options1[]" value = "No" style="width: 20px" >  No&nbsp; </LABEL>
              </td>
              <td class="auto-style1" style="width: 69px">
              <LABEL> <INPUT TYPE="checkbox" NAME="options2[]" value = "Yes" style="width: 20px" > Yes</LABEL><br/>
              <LABEL> <INPUT TYPE="checkbox" NAME="options2[]" value = "No" style="width: 20px" > No&nbsp; </LABEL>
              </td>
              <td class="auto-style1" style="width: 53px">
              <LABEL> <INPUT TYPE="checkbox" NAME="options3[]" value = "Yes" style="width: 20px" > Yes</LABEL><br/>
              <LABEL> <INPUT TYPE="checkbox" NAME="options3[]" value = "No" style="width: 20px" > No&nbsp; </LABEL>
              </td>
              <td class="auto-style1" style="width: 53px">
              <LABEL> <INPUT TYPE="checkbox" NAME="options4[]" value = "Yes" style="width: 20px" > Yes</LABEL><br/>
              <LABEL> <INPUT TYPE="checkbox" NAME="options4[]" value = "No" style="width: 20px" > No&nbsp; </LABEL>
              </td>
              <td style="width: 77px" class="auto-style1">
              <LABEL> <INPUT TYPE="checkbox" NAME="options5[]" value="Yes" style="width: 20px" > &nbsp; </LABEL>
              </td>
              <td style="width: 76px" class="auto-style1">
              <LABEL> <INPUT TYPE="checkbox" NAME="options6[]" value="Yes" style="width: 20px" > &nbsp; </LABEL>
              </td>
              <td style="width: 64px" class="auto-style1">
              <LABEL> <INPUT TYPE="text" NAME="text0" VALUE="" > &nbsp; </LABEL>
              </td>
                <td style="width: 152px" class="auto-style2">
                <textarea name="comments0" style="width: 153px; height: 127px;" cols="20" rows="1">
              </textarea><br/>
                </td>
              </tr>

<input type = "submit" name = "submit" value = "click me" />
      </form>

php file.

$v1 = $_POST['options0'];
$v2 = $_POST['options1'];
$v3 = $_POST['options2'];
$v4 = $_POST['options3'];
$v5 = $_POST['options4'];
$inp = $_POST['options5'];
$ninp = $_POST['options6'];
$nd = $_POST['text1'];
$comment0 = $_POST['comments0']

mysql_query("insert into table(id, v1, v2, v3, v4, v5, Inp, NInp, ND, comments) values ('','$v1[0]','$v2[0]','$v3[0]','$v4[0]','$v5[0]','$inp[0]','$ninp[0]','$nd','$comment0')");
SOLUTION
Avatar of Duboux
Duboux

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Why don't you use radio buttons for yes/no answers ? This seems a little strange having the choice to select yes and no. And you would store only one simple value in the db.
The checkboxes are able to be selected both for the (example) option1, so what's in the database when you select both of them ?
Avatar of ichiragpatel
ichiragpatel

ASKER

what i have done is i created one array options0[] to save one value "yes" or "no" into db table.
and options1[] to save one value"yes" or"no" into db table.
and so on.
so in my db table i have 9 table columns to save value of each options[] arrays.
thats why i have 9 different arrays.
I suppose you already have the adding the info into your database part working for you..

Did my solution about the showing it back, work for you ?
there you go,


$sql = 'SELECT * FROM table_name WHERE id = 1';
      $r = mysql_query($sql, $database);
      
      $row = mysql_fetch_assoc($r);
      $options0 = $row['v1'];
      $options1 = $row['v2'];
      $options2 = $row['v3'];
      $options3 = $row['v4'];
      $options4 = $row['v5'];
      $options5 = $row['inp'];
      $options6 = $row['ninp'];
Duboux, thank you. but as you said i can save my data in to dabase.
but the problem is suppose i want to get the saved checkbox values in to the form, i cant get those.
if i want to view checkbox values, it is simple but lets say if i want to update some checkbox values i need to get those into the form first.
plz help.
thank you.
When a checkbox is checked, it passes on its VALUE property to the web server - as you would expect. The problem is that when it is unchecked, nothing passed to the web server indicates that checkbox ever existed! you can use this behavior to set just checkboxes having values.

options0[] = Yes (if checked)
options0[] = No  (if checked)

also, why do you need two check boxes for yes and no? One checkbox is just enough (checked-yes, unchecked-no)

You save in database the first one value: $v1[0]. But, you should take into account when the checkbox is unchecked - no value is returned. I will suppose that you did proper coding and you will save in database value $v1 as true or false (true for yes, false for no).

Please, this coding is just guidelines of this principle.
<?php
$ch1_yes ='unchecked';
$ch1_no = 'unchecked';

if ($v1 == true) {
$ch1_yes ='checked';
}else{
$ch1_no ='checked';
}
?>

<td class="auto-style1" style="width: 69px">
              <LABEL> <INPUT TYPE="checkbox" NAME="options0[]" value =" Yes "
<?php print $ch1_yes; ?> style="width: 20px" > Yes</LABEL><br/>
              <LABEL> <INPUT TYPE="checkbox" NAME="options0[]" value =" No " <?php print $ch1_no; ?>style="width: 20px" > No&nbsp; </LABEL>
</td>

Open in new window

My whole 1st post was about showing the values from your database in the form.

What would u like to see more ?
thanks guys..for helping me..
in my first post, where i have shown that i have checkboxes from [0..6].
suppose this checkboxes are in single row and in 6 columns. each column has two checkboxes, let's say option[0]-yes
option[0]-no
respectively for other columns.
now what i do is i get every checkbox value in variable to print the value of that checkbox. so it means i put 6 values in 6 different variables.
and i have 30 rows filled with checkboxes. so that means i have to use 180 variables aprox.
it is time consuming and space too..
is there any way to cut this time and space..
can you guys suggest me something.
can looping  be an option..
thank you..

You do not need different variables. You can name each check box as options[], but then just have different values: Yes00, No00, Yes01, No01,.... (YesRowNoColNo)
In this way, all checkbox values will be placed in one array. Having this, you can identify from where each value comes: YesRowNoColNo (yes or no, row number and column number).

But just to point, regardless of number of checkboxes in form, if just one is checked, you will get only that value in posted array.
hi ezov,

i am little confuse about the concept still.
please elaborate little more.
thank you.
Here is the example which is very simple and I suppose you need. Name the file process.php and try it.
all processing is here:
$chk_values = $_POST["options"];
foreach ($chk_values as $v) {
    echo "Current value of \$chk_values: $v.<br>";
    $$v = 'checked';
}

So, in a database you just save $chk_values. If you want to retrieve checked boxes in your form, retrieve variable from database.
This form simulates this process: it reconstructs submitted values. You can expand this form to any number of rows or columns. I used convention here {Yes}0102 (first two digits for row, other two for cocumns)  just for clarity.The value can be any string, as long as it is unique in the form.
<html><head>

<title>Check Boxes</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head><body bgcolor="#ffffff" text="#000000">

<?php

$chk_values = $_POST["options"];
foreach ($chk_values as $v) {
    echo "Current value of \$chk_values: $v.<br>";
    $$v = 'checked'; 
}

?>

<form name="form1" method="post" action="process.php"> 
 <table width="100%" border="0" bgcolor="#D6E3F7">
<tr>
<td class="text">Your are Expertise field: </td>
</tr>
<tr>
<td class="examplelink">
<input type="checkbox" name="options[]" value="Yes0101" <?php print $Yes0101; ?>>
Yes
<input type="checkbox" name="options[]" value="No0102" <?php print $No0102; ?>>
No
<input type="checkbox" name="options[]" value="Yes0103" <?php print $Yes0103; ?>>
Yes
<input type="checkbox" name="options[]" value="No0104" <?php print $No0104; ?>>
No
</td>
</tr>
<tr>
<td class="examplelink">
<input type="checkbox" name="options[]" value="Yes0201" <?php print $Yes0201; ?>>
Yes
<input type="checkbox" name="options[]" value="No0202" <?php print $No0202; ?>>
No
<input type="checkbox" name="options[]" value="Yes0203" <?php print $Yes0203; ?>>
Yes
<input type="checkbox" name="options[]" value="No0204" <?php print $Yes0204; ?>>
No
</td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit">
<input name="hidSubmit" type="hidden" id="hidSubmit2" value="true"></td>
</tr>
<tr>
<td class="reditalic">&nbsp; </td>
</tr>
</table>
</form>
</body></html>

Open in new window

Here is the screen shot of this script.
ScreenShot505.jpg
nice ezov..
thanks man..
very helpful....
so guys i have to create view page same like index page to see the retrieved result.
am i right?
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.