Forum Moderators: coopster
If your checkbox has the same name attribute, then add [] to the name
<input type="checkbox" name="test[]" value="1">
<input type="checkbox" name="test[]" value="2">
<input type="checkbox" name="test[]" value="3">
If all checkbox are checked, it will return an array as follow array("1","2","3").
In PHP, you should:
1. Check that POST variable exist, if not it's an empty array
if(isset($_POST["test"])) {$test = $_POST["test"];} else {$test=array();}
2. Then explode the array and check every single element using a loop (safer since you want to insert it in a database). Also, you can unset all empty element.
for ($i="0"; $i<count($test); $i++) {
if(!is_numeric($test[$i])) {$test[$i]="";}
if(empty($test[$i])) {unset($test[$i]);}}
3. Then store it in the database - turn you array into a string. I like to store array in my database with "<>" as a separator (easier to explode/implode). There are many other different ways to store your data in your database, you can use whatever you want but note that empty space is not the best option if values have space (e.g. "test 1" instead of "1")
$test = implode ("<>", $test);
$test = "<>".$test."";
$sql = "INSERT INTO test_table (`id`, `test_value`) VALUES (NULL, '$test')";
$res=mysql_query($sql) or die ("Fail to add test_value");
4. Retrieve stored data using explode - this will turn your string into an array again.
$sql = "select id, test_value from test_table limit 1";
$res=mysql_query($sql) or die ("Fail to get test");
$test = explode('<>',$pres[1]);
for ($i="0"; $i<count($test); $i++) {
if(empty($test[$i])) {unset($test[$i]);} }
5. Form - Automatically check boxes which has been checked - Just use in_array to see if current test[] value has been checked.
echo '<input type="checkbox" name="test[]" value="1";
if(in_array("1", $test)) {echo ' checked';}
echo '><input type="checkbox" name="test[]" value="2";
if(in_array("2", $test)) {echo ' checked';}
echo '><input type="checkbox" name="test[]" value="3";
if(in_array("3", $test)) {echo ' checked';}
echo '>';
That's it!
Tomda
<form name="services" action="addservices.php" method=post>
<table border=1 cellspacing=1 cellpadding=1 width=70% bordercolor='#0000ff' align="center">
<tr>
<td>
<input type="checkbox" name="services[]" value="1">Illumination Tour
<input type="checkbox" name="services[]" value="2">Provins, a Medieval City
</td>
<td>
<input type="checkbox" name="services[]" value="3">Wine tasting
<input type="checkbox" name="services[]" value="4">Private Walking Tour of Paris
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="services[]" value="5">Chateaux de Versailles
<input type="checkbox" name="services[]" value="6"> Chantilly
</td>
<td>
<input type="checkbox" name="services[]" value="2">Bateaux Mouches
<input type="checkbox" name="services[]" value="8">Giverny & Monet's Garden
</td>
</tr>
</table>
And here is my form which is for retriving values
<?
if(isset($_POST["services"]))
{
$services = $_POST["services"];
}
else
{
$services=array();
}
$qrySel = "INSERT INTO test (bookingid, services) values('$bookingid', '$services')";
echo $qrySel;
$result = mysql_query($qrySel);
?>
</form>
The problem is get the values of checkboxes as array. I mean it is jsut priting array.
Any help will be appreciated
Warm Regards
Also, you may want to have a look at the PHP methods serialize [php.net] and unserialize [php.net], which make array-to-string conversions and back easier.
Thanks again, Now i have different situation similar to this, i have a field in DB called bookingstatus whose value will be <>1<>2<>3
Now after certain action I want to add one more extra value i.e. <>1<>2<>3<>4 how can i update this, should i have to check the previous values? how do i do update?
Warm Regards