Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

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')");
0
ichiragpatel
Asked:
ichiragpatel
  • 6
  • 5
  • 3
  • +2
2 Solutions
 
DubouxCommented:
So u mean the other way around...

k, say u get 1 record from the database table (for the example I use the LIMIT 1)
$Result = mysql_query("SELECT * FROM table LIMIT 1");
$row = mysql_fetch_array($Result, MYSQL_ASSOC);

// This will give you all the form values in the array $row
// There's 2 types of form fields: options/selections and textfields

// First the options. You could make a general used function that checks the value and checks the appropriate box.

function is_selected($option_number, $value) {
      global $row;
      if (isset($row["v".$option_number])) {
            $output  =  (strtolower($row["v".$option_number]) == strtolower($value))  ?  " CHECKED"  :  "";
      }
      else {
            $output = "";
      }
      return $output;
}

// And then call it in each checkbox line:

<LABEL> <INPUT TYPE="checkbox" NAME="options1[]" value = "Yes" style="width: 20px" <?= is_selected(1, "Yes") ?>> Yes</LABEL><br/>
<LABEL> <INPUT TYPE="checkbox" NAME="options1[]" value = "No" style="width: 20px" <?= is_selected(1, "No") ?>>  No&nbsp; </LABEL>

<LABEL> <INPUT TYPE="checkbox" NAME="options2[]" value = "Yes" style="width: 20px" <?= is_selected(2, "Yes") ?>> Yes</LABEL><br/>
<LABEL> <INPUT TYPE="checkbox" NAME="options2[]" value = "No" style="width: 20px" <?= is_selected(2, "No") ?>> No&nbsp; </LABEL>


For the form fields that include only text, you can just have it as value (if it exists)

<?php $text1  =  (isset($row["ND"]))  ?  $row["ND"]  :  ""; ?>
<LABEL> <INPUT TYPE="text" NAME="text0" VALUE="<?= $text1 ?>" > &nbsp; </LABEL>

// and
<?php $comments0  =  (isset($row["comments"]))  ?  $row["comments"]  :  ""; ?>
<textarea name="comments0" style="width: 153px; height: 127px;" cols="20" rows="1"><?= $comments0 ?>
</textarea><br/>
0
 
Lukasz ChmielewskiCommented:
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.
0
 
Lukasz ChmielewskiCommented:
The checkboxes are able to be selected both for the (example) option1, so what's in the database when you select both of them ?
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
ichiragpatelAuthor Commented:
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.
0
 
DubouxCommented:
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 ?
0
 
adeelshahidCommented:
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'];
0
 
ichiragpatelAuthor Commented:
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.
0
 
eZovCommented:
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

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

What would u like to see more ?
0
 
ichiragpatelAuthor Commented:
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..

0
 
eZovCommented:
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.
0
 
ichiragpatelAuthor Commented:
hi ezov,

i am little confuse about the concept still.
please elaborate little more.
thank you.
0
 
eZovCommented:
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

0
 
eZovCommented:
Here is the screen shot of this script.
ScreenShot505.jpg
0
 
ichiragpatelAuthor Commented:
nice ezov..
thanks man..
very helpful....
0
 
ichiragpatelAuthor Commented:
so guys i have to create view page same like index page to see the retrieved result.
am i right?
0
 
eZovCommented:
This array ($_POST["options"];) has results from checkboxes:


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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now