Solved

how do i retrieve checkbox values from database using php

Posted on 2011-03-04
17
510 Views
Last Modified: 2013-12-13
<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
Comment
Question by:ichiragpatel
  • 6
  • 5
  • 3
  • +2
17 Comments
 
LVL 3

Assisted Solution

by:Duboux
Duboux earned 100 total points
ID: 35041507
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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35042091
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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35042108
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
 

Author Comment

by:ichiragpatel
ID: 35044693
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
 
LVL 3

Expert Comment

by:Duboux
ID: 35048959
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
 
LVL 2

Expert Comment

by:adeelshahid
ID: 35056911
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
 

Author Comment

by:ichiragpatel
ID: 35059390
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
 
LVL 2

Expert Comment

by:eZov
ID: 35073312
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 3

Expert Comment

by:Duboux
ID: 35073897
My whole 1st post was about showing the values from your database in the form.

What would u like to see more ?
0
 

Author Comment

by:ichiragpatel
ID: 35088328
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
 
LVL 2

Expert Comment

by:eZov
ID: 35091108
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
 

Author Comment

by:ichiragpatel
ID: 35091789
hi ezov,

i am little confuse about the concept still.
please elaborate little more.
thank you.
0
 
LVL 2

Expert Comment

by:eZov
ID: 35093550
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
 
LVL 2

Expert Comment

by:eZov
ID: 35093563
Here is the screen shot of this script.
ScreenShot505.jpg
0
 

Author Comment

by:ichiragpatel
ID: 35098797
nice ezov..
thanks man..
very helpful....
0
 

Author Comment

by:ichiragpatel
ID: 35117170
so guys i have to create view page same like index page to see the retrieved result.
am i right?
0
 
LVL 2

Accepted Solution

by:
eZov earned 400 total points
ID: 35117185
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
phpmyadmin 3 33
Php pie charts 3 26
Regular Expression Calculator Tester 2 54
Auto-indent certain lines in Notepad++ 10 23
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now