Pulling Checkbox data from a table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
funkymeerkat
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 4:16 am

Pulling Checkbox data from a table

Post by funkymeerkat »

Hi, i have created a form which sends data to a table...

The form is a checklist, and i want the form to be filled out gradually throughout the day. Therefore i would expect that someone may check one box and submit and then reopen the form later to check another. Therefore, when they open the form later i require the one they have already checked to display as checked. I don't want to use cookes/sessions anything like that though. I need to pull the "checked data" from a table.

Can this be done and if so can someone give me an indication on how i can echo this data out of my table back into the form depending on whether it was checked or not.

Any help would be much appreciated.

Cheers,

Paul
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Pulling Checkbox data from a table

Post by papa »

Create a field in the db table that stores a value if checked.

Then when you fetch the data you simply see if that field has a value and echo "checked=\"checked\" in the checkbox.

Code: Select all

 
!empty($check_field) ? $checked = "checked=\"checked\"" : $checked="";
 
echo "<input type=\"checkbox\" name=\"foo\" $checked />\n";
Something like that...
funkymeerkat
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 4:16 am

Re: Pulling Checkbox data from a table

Post by funkymeerkat »

Hi thanks for the reply.

I sort of understand but seem to see how to fit it into my code...

For example if my code is the following....

<?php
include("includes/login_details.inc");
mysql_connect ($dbhost, $dbuser, $dbpass) or die (mysql_error()); //Connects to database
mysql_select_db ($dbname) or die (mysql_error()); //Selects your database
$sql = "SELECT * FROM passover where date = '$date' and shift = 'DAY'";

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo "<font size=2 face=Tahoma>There is a problem. There is no passover form created for today's date. Please try again, or use a paper copy.</font>";

}

while ($row = mysql_fetch_assoc($result)) {
echo "<form method=post action=passover-confirm.php onSubmit='return checkrequired(this)' >

<table width='650' border='0' cellspacing='3' cellpadding='3'><tr>
<td width=275><font size=1 face=Verdana, Arial, Helvetica, sans-serif>Check
Previous Passover Form</font></td>
<td width=50><font size=1 face=Verdana, Arial, Helvetica, sans-serif>
<input type=checkbox name=previous_passover value=checkbox>
</font></td>
<td width=275><font size=1 face=Verdana, Arial, Helvetica, sans-serif>Check
4730 is AUTO-IN on Frt Dsk Lft (8am)</font></td>
<td width=50><font size=1 face=Verdana, Arial, Helvetica, sans-serif>
<input type=checkbox name=checkbox3 value=checkbox>
</font></td>
</tr>
</table>
</form>";
}
mysql_free_result($result);
mysql_close();
?>

Where would you add that so that it echos the info...

Bearing in mind i am already displaying data from a table as above. How would i parse the info in...

I thought perhaps like....

<tr>
<td width=275><font size=1 face=Verdana, Arial, Helvetica, sans-serif>Check
Previous Passover Form</font></td>
<td width=50><font size=1 face=Verdana, Arial, Helvetica, sans-serif>
<input type=checkbox name=previous_passover value=checkbox>
</font></td>
<td width=275><font size=1 face=Verdana, Arial, Helvetica, sans-serif>Check
4730 is AUTO-IN on Frt Dsk Lft (8am)</font></td>
<td width=50><font size=1 face=Verdana, Arial, Helvetica, sans-serif>
<input type=checkbox name=checkbox3 value=checkbox>
</font></td>
</tr>
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Pulling Checkbox data from a table

Post by aceconcepts »

Where would you logically put it yourself?

Do a bit of trial and error - it's a good way to learn :D
funkymeerkat
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 4:16 am

Re: Pulling Checkbox data from a table

Post by funkymeerkat »

Sorry never put where i'd put it (got caught up at work...)

To be honest i do have a reasonable grasp of php, but i have never actuall tried to echo out these values so i really don't know.

I have trid what the guy said in my code and nothing seems to work...

Although i dont get any errors....
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Pulling Checkbox data from a table

Post by aceconcepts »

Well, you could run the check immediately after the while loop code:

Code: Select all

 
while ($row = mysql_fetch_assoc($result)) {
$checked="";
!empty($check_field) ? $checked = "checked=\"checked\"" : $checked="";
 
Next, all you need to do is add $checked variable to the form element:

Code: Select all

 
<input type=checkbox name=previous_passover value=checkbox <? echo $checked; ?>>
 
funkymeerkat
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 4:16 am

Re: Pulling Checkbox data from a table

Post by funkymeerkat »

Have tried that with no luck at all....

Been doing abit of investigation and thought i might be getting somewhere with this....

<td width=50><font size=1 face=Verdana, Arial, Helvetica, sans-serif>";
echo "<input type=checkbox name=previous_passover value=Yes";
if ($previous_passover=="Yes")
echo " Checked";
else
echo "";
echo ">
</font></td>

It doesnt work properly although i've been playing around with it a bit now....

Im sure its something really stupid thats staring me in the face, but just can't see it at the moment.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Pulling Checkbox data from a table

Post by papa »

It's hard to tell with that code as it's parse errors everywhere. Please give us a little bit more. :)

Good practise is to write checked="checked" isntead of checked.
funkymeerkat
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 4:16 am

Re: Pulling Checkbox data from a table

Post by funkymeerkat »

Hi,

This is the whole page....

The particular problem i have highlighted red..

Code: Select all

<?php
 
$date=$_GET['date'];
 
 
include("includes/login_details.inc");
 
mysql_connect ($dbhost, $dbuser, $dbpass) or die (mysql_error()); //Connects to database 
 
mysql_select_db ($dbname) or die (mysql_error()); //Selects your database 
 
$sql = "SELECT * FROM nightstaffpassover where date = '$date' and shift = 'DAY'";
 
 
 
    $result = mysql_query($sql);
    
    //echo $sql;
 
    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        exit;
    }
    
    if (mysql_num_rows($result) == 0) {
        echo "<font size=2 face=Tahoma>There is a problem. There is no passover form created for today's date. Please try again, or use a paper copy.</font>";
 
    }
 
    // While a row of data exists, put that row in $row as an associative array
    // Note: If you're expecting just one row, no need to use a loop
    // Note: If you put extract($row); inside the following loop, you'll
    //       then create $userid, $fullname, and $userstatus
    while ($row = mysql_fetch_assoc($result)) 
    
    {
     
        echo "<form method=post action=nightday-passover-confirm.php onSubmit='return checkrequired(this)' ><table width='100%' border='0' cellspacing='1' cellpadding='1'>
  <tr>
    <td>
<table width='650' border='0' cellspacing='3' cellpadding='3'>
  <tr> 
    <td><font size='4' color='#3399FF' face='Arial, Helvetica, sans-serif'><strong>NIGHTSTAFF 
      <font color='#FF0000'>DAYSHIFT</font> PASSOVER FORM</strong></font></td>
  </tr>
</table>
<table width='650' border='0' cellspacing='3' cellpadding='3'>
  <tr>
    <td background='images/seperator.png'>&nbsp;</td>
  </tr>
</table>
<table width=650 border=0 cellspacing=3 cellpadding=3>
  <tr> 
    <td width=275><font color='#3399FF' size='1' face='Verdana, Arial, Helvetica, sans-serif'><strong>TIME 
      SPECIFIC <input type=hidden name=date value=";
    echo $row['date'];
    echo ">
      "; echo "
      <input type=hidden name=id value=";
    echo $row['id'];
    echo ">
      ";  echo "</strong></font></td>
    <td width=50><font size=1 face=Verdana, Arial, Helvetica, sans-serif>&nbsp; 
      </font></td>
    <td width=275><font size=1 face=Verdana, Arial, Helvetica, sans-serif>&nbsp;</font></td>
    <td width=50><font size=1 face=Verdana, Arial, Helvetica, sans-serif>&nbsp; 
      </font></td>
  </tr><tr> 
    <td width=275><font size=1 face=Verdana, Arial, Helvetica, sans-serif>Check 
      Previous Passover Form</font></td>
    <td width=50><font size=1 face=Verdana, Arial, Helvetica, sans-serif>";
[color=#FF0000]echo "<input type=checkbox name=previous_passover value=Yes";
if ($previous_passover==Yes)
echo " Checked>"; 
else
echo "";
echo ">[/color]
      </font></td>
    <td width=275><font size=1 face=Verdana, Arial, Helvetica, sans-serif>Check 
      4730 is AUTO-IN on Frt Dsk Lft (8am)</font></td>
    <td width=50><font size=1 face=Verdana, Arial, Helvetica, sans-serif> ";
[color=#FF0000]echo "<input type=checkbox name=check_4730 value=Yes";
if ($check_4730==Yes)
echo " Checked>"; 
else
echo "";
echo ">[/color]
      </font></td>
  </tr>
</table>
<br>
<table width='200' border='0' cellspacing='3' cellpadding='3'>
  <tr> 
    <td><font color='#3399FF' size='1' face='Verdana, Arial, Helvetica, sans-serif'><strong>MONITORING 
      ISSUES TO PASSOVER</strong></font></td>
  </tr>
  <tr> 
    <td><textarea name='textarea3' cols='100' rows='4' wrap='VIRTUAL' id='textarea3' STYLE='font-family: verdana; font-size:7pt; color:000066; background-color:ffffFF; border-top: 2px #71B8FF solid; border-left: 2px #71B8FF solid; border-bottom: 2px #71B8FF solid; border-right: 2px #71B8FF solid'></textarea></td>
  </tr>
</table>
 
<br>
<table width='200' border='0' cellspacing='3' cellpadding='3'>
  <tr> 
    <td><font color='#3399FF' size='1' face='Verdana, Arial, Helvetica, sans-serif'><strong>SERVER 
      REBOOTS BY NIGHTSTAFF</strong></font></td>
  </tr>
  <tr> 
    <td><textarea name='callpassovernotes' cols='100' rows='4' wrap='VIRTUAL' id='callpassovernotes' STYLE='font-family: verdana; font-size:7pt; color:000066; background-color:ffffFF; border-top: 2px #71B8FF solid; border-left: 2px #71B8FF solid; border-bottom: 2px #71B8FF solid; border-right: 2px #71B8FF solid'></textarea></td>
  </tr>
</table>
<br>
<table width='200' border='0' cellspacing='3' cellpadding='3'>
  <tr> 
    <td><font color='#3399FF' size='1' face='Verdana, Arial, Helvetica, sans-serif'><strong>NON-TECHNICAL 
      ISSUES TO PASSOVER</strong></font></td>
  </tr>
  <tr> 
    <td><textarea name='nontech' cols='100' rows='2' wrap='VIRTUAL' id='nontech' STYLE='font-family: verdana; font-size:7pt; color:000066; background-color:ffffFF; border-top: 2px #71B8FF solid; border-left: 2px #71B8FF solid; border-bottom: 2px #71B8FF solid; border-right: 2px #71B8FF solid'></textarea></td>
  </tr>
</table>
<table width='650' border='0' cellspacing='3' cellpadding='3'>
  <tr>
    <td background='images/seperator.png'>&nbsp;</td>
  </tr>
</table>
      <br>
      <table width='200' border='0' cellspacing='3' cellpadding='3'>
        <tr>
          <td><div align='center'>
              <input type='submit' name='Submit2' value='REVIEW & SUBMIT PASSOVER FORM' STYLE='WIDTH: 300px; HEIGHT: 25px; font-family: verdana; font-size:7pt; color:ffffff; font-weight: bold; background-color:3399FF; border-top: 2px #3399FF solid; border-left: 2px #3399FF solid; border-bottom: 4px #3399FF solid; border-right: 2px #3399FF solid'>
            </div></td>
        </tr>
      </table> </td>
  </tr>
</table></form>";
 
    }
        
    mysql_free_result($result);
 
mysql_close();
 
 
?>
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Pulling Checkbox data from a table

Post by papa »

What is this table column called?
# if ($previous_passover==Yes)
# echo " Checked>";

You don't get any data from the db. Like so:
echo $row['id'];

if($row['passover'] == "Yes")

Either way, you need to use "".
funkymeerkat
Forum Newbie
Posts: 18
Joined: Sun Jun 13, 2004 4:16 am

Re: Pulling Checkbox data from a table

Post by funkymeerkat »

I totally get it now and it works a treat!

Excellent - thanks so much....
Post Reply