2.) How to pull this stored checkbox data so i can display which checkboxes
are checked and which not ?
> 1.) what is the best way to store multiple checkboxes into table. (e.g. if
> you 20 checkboxes there is no reason to make 20 table fields ? )
I would serialize an array and store the serialized string in the DB.
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="checkbox" name="cb[1]" value="on" checked="checked" />
<input type="checkbox" name="cb[2]" value="on" checked="checked" />
<input type="checkbox" name="cb[3]" value="on" />
<input type="checkbox" name="cb[4]" value="on" checked="checked" />
<input type="checkbox" name="cb[5]" value="on" />
<input type="submit" value="submit →"/>
</form>
<?php
/*
POST data looks like this...
Array
(
[cb] => Array
(
[1] => on
[2] => on
[4] => on
)
)
*/
echo serialize( $_POST['cb'] );
// a:3:{i:1;s:2:"on";i:2;s:2:"on";i:4;s:2:"on";}
> 2.) How to pull this stored checkbox data so i can display which checkboxes
> are checked and which not ?
$cbdata = unserialize( $data ); // will give you the checked boxes as
an array.
--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Depends on what you want. There might well want to make twenty fields, or
not.
> 2.) How to pull this stored checkbox data so i can display which checkboxes
> are checked and which not ?
Depends on how/where you're storing the data. You have to pull all the data
so you can check the boxes or not.
I could understand if you thought this not helpful, but really you're not
asking specific enough questions to be able to give what I'd call helpful
answers.
Mark
I am new to php also and ran in to the same question. I have 2 rows and
26 columns for chars A->Z. This code is in a FORM NAME="author_abbrev"
and I POST the action to $_SERVER['PHP_SELF']. You can see it at
http://ichbin.9999mb.com if the free hosting site is running.
print '<table><tbody><tr>';
$loopValue = 1;
//
// Loop thru and build the 26 radio buttons with the correct
selected one
foreach (range('A', 'Z') as $letter)
{
if( $loopValue == 14)
{
print '</tr><tr>';
}
print '<td><input type="radio" name="abbr_letter" value="'.
$letter .'" ';
if( $letter == $_POST['abbr_letter']) print 'checked="checked" ';
print ' onclick="submit()" />'.$letter.'</td>';
$loopValue = $loopValue +1;
}
print '</tr></tbody></table>';
--
Thanks in Advance... http://ichbin.9999mb.com
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
This is what I had come up with, using arrays when in the script and
varchar in the DB, you get all the related boxes into one field, and
have human readable represntation of the contents.
Here's the linik:
http://www.portcommodore.com/quickpage.php?id=mixed+selection+lists
(this page is a work in progress, sorry for any errors)
Larry
There are several reasons for doing this:
1. Your person table doesn't get clogged up with a bunch of fields
2. You can change the checkbox names with screwing up existing data.
3. You can easily retrieve the information using joins or other
apporpiate query.
When I display the checkboxes in the form, I do it with the code shown
below. This script gets the checkbox names from the checkbox table and
displays them in an array in a table cell, adding a new cell whenever
it needs to to create a new column. This is set up for 3 columns. You
can change the code for various numbers of columns depending on how
many checkboxes you have.
If you want to show which checkboxes have been checked for which person
IDs, you can derive that data by selecting the checkbox IDs on the
appropriate person ID from the linking table and using that information
to get the names of the checked checkboxes from the checkbox table and
then inserting that between the checkbox tags in the html using
$_POST[].
--Kenoli
//::::::::::::::: Create interest area checkboxes. :::::::::::::::::://
<?php
function interest_checkboxes() {
echo "<fieldset class='callborder'><legend class=\"title\">Interest
Area</legend>\n";
echo "<table class=\"callborder\"><tr>\n<td>";
global $row;
$query = "SELECT * FROM tlu_interest_area ORDER BY interest_area
ASC";
$result = @mysql_query($query);
$number = mysql_num_rows($result);
$i=0;
$second_col = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($i > ($number/3) && $second_col == 0) {
echo "</td><td>";
$second_col = 1;
}
elseif ($i > (2*$number/3) && $second_col == 1) {
echo "</td><td>";
$second_col = 2;
}
if (in_array('6', $interest)) {$checked = 'checked';} else {$checked
= '';}
echo "<input class=\"checkbox\" type=checkbox $checked
name=\"interests[]\" value=\"{$row[interest_area_id]}\"
/>{$row[interest_area]}</input><br/>\n";
$i ++;
}
echo "</td></tr></table></fieldset>";
}
?>
I see where you are going with it, that's prety slick.
One of the benefits of the arrays concept is you can group the
checkboxes, but I gureess in the checkbox table you could put a
groupname there too.
?>
<form action="testCheckBox.php" name="test" method="post">
<?
$weekdays = array( "mon" => "Monday",
"tue" => "Tuesday",
"wed" => "Wednesday",
"thr" => "Thursday",
"fri" => "Friday",
"sat" => "Saturday",
"sun" => "Sunday" );
// initialize $days as being empty:
$days = array();
echo "<b>Days of Service:</b><br />";
foreach( $weekdays as $value => $desc ) { //walk through possible values
echo ' <INPUT type="checkbox" name=days[]" value="'.$value.'"';
echo ( in_array( $value,$days ) ) ? " CHECKED" : ""; //if days are
already selected, check the box
echo '>'.$desc.'<br />'; //echo the descriptive name of the item
}
$daylist = implode(",",$days);
//$result = mysql_query("UPDATE dbTest SET db_days = '$daylist' WHILE
record_id = $record_id");
$days = explode(",", $dbTest ['db_days']);
?>
<input type="submit" name="Submit" value="Submit">
</form>
don't need this ?>
> <form action="testCheckBox.php" name="test" method="post">
> <?
> $weekdays = array( "mon" => "Monday",
> "tue" => "Tuesday",
> "wed" => "Wednesday",
> "thr" => "Thursday",
> "fri" => "Friday",
> "sat" => "Saturday",
> "sun" => "Sunday" );
>
> // initialize $days as being empty:
> $days = array();
>
> echo "<b>Days of Service:</b><br />";
> foreach( $weekdays as $value => $desc ) { //walk through possible values
> echo ' <INPUT type="checkbox" name=days[]" value="'.$value.'"';
> echo ( in_array( $value,$days ) ) ? " CHECKED" : ""; //if days are
> already selected, check the box
> echo '>'.$desc.'<br />'; //echo the descriptive name of the item
> }
>
> $daylist = implode(",",$days);
> //$result = mysql_query("UPDATE dbTest SET db_days = '$daylist' WHILE
> record_id = $record_id");
> $days = explode(",", $dbTest ['db_days']);
> ?>
> <input type="submit" name="Submit" value="Submit">
> </form>
>
>
----------------------------------------------------------
<form action="testCheckBox.php" name="test" method="post">
<?
$weekdays = array( "mon" => "Monday",
"tue" => "Tuesday",
"wed" => "Wednesday",
"thr" => "Thursday",
"fri" => "Friday",
"sat" => "Saturday",
"sun" => "Sunday" );
// initialize $days as being empty:
$days = array();
echo "<b>Days of Service:</b><br />";
foreach( $weekdays as $value => $desc ) { //walk through possible values
echo ' <INPUT type="checkbox" name=days[]" value="'.$value.'"';
echo ( in_array( $value,$days ) ) ? " CHECKED" : ""; //if days are
already selected, check the box
echo '>'.$desc.'<br />'; //echo the descriptive name of the item
}
$daylist = implode(",",$days);
//$result = mysql_query("UPDATE dbTest SET db_days = '$daylist' WHILE
record_id = $record_id");
$days = explode(",", $dbTest ['db_days']);
?>
<input type="submit" name="Submit" value="Submit">
</form>
------------------------------------------------------------------------
The code you posted will return to the form action target an array:
days (if any days are checked) in $_POST. This is a code snippet not
the entire form logic, You still have to validate the POST data,
implode and use a DB INSERT/UPDATE statement to finish it all.
Here is some more code for how to bring the form data in:
//this will get your days back to a local array (when you read in your
POSTed from data, if there is no data it set up an empty array (in case
you go back to the entry form)
If isset($_POST['days']) {
$days = $_POST['days'];
} else {
$days = array();
}
// Add validation code here...
//If the data is valid, before you put it in SQL you will need to make
your array into a string (again you should use addslashes escape any
potentially bad data.)
$sqldays = implode(",",$days);
//(you would add the result string into your update/insert query for
the days field.)
//then when reading in back in from SQL (for editing):
$days = explode(",",$sqlresult['daysfield']);
As for complete form logic,
Here is a write up of how to code a one file form/db handling script
(not suited for everything but is good for single record entry/edit
work.)
http://www.portcommodore.com/quickpage.php?id=Single+File+Edit+Script
(I havent posted the example script, should do that soon.)
Larry