Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox array

Status
Not open for further replies.

1083152

IS-IT--Management
May 17, 2001
2
NL
Hi iam useing a varible number of checkboxes wich values are pulled out of a database. I then have to submit those values to an other table

here is the script fot getting the checkboxes
?php
$result = mysql_db_query ("setf","select ond_id, titel from onderwerpen ");
?>
Onderwerpen waar het mee te maken heeft:<br>
<?php
while ($myrow = mysql_fetch_array($result)){
?>
<INPUT name=&quot;ond_id&quot; type=&quot;checkbox&quot; value=&quot;<?php echo $myrow[&quot;ond_id&quot;] ?>&quot;> <?php echo $myrow[&quot;titel&quot;] ?>
<?php
}

$ond_id this is an array how do i take apart this array and insert the multible values into the database?

thanx in advance
 
It really depends on how you want to use it:

I would recommend using an array name for your checkbox name, so that all the CHECKED values form an array when the form is submitted:

<INPUT name=&quot;ond_id[]&quot;

This gives you more control over what you can do when the form submits. Often what I will do in the form handler is just take the array of values and concatenate them together, separated by a comma, so that I can place them all in one record:

Code:
<?php
$ond_choices = implode(&quot;,&quot;,$ond_id)
So you can place a value in a record for all the choices checked by that person. Then, when you want to extract that value into the form, and populate a group of checkboxes from that record, you simply expand that comma-separated record back into an array, and for each row in the output array, you check to see if that value is in the array of checked choices:

Code:
<?php

$checked_values = explode(&quot;,&quot;,$ond_choices); //creates array of checked items

 $result = mysql_db_query (&quot;setf&quot;,&quot;select ond_id, titel from onderwerpen &quot;);
 ?>
 Onderwerpen waar het mee te maken heeft:<br>
 <?php 
 while ($myrow = mysql_fetch_array($result)){
?>
    <INPUT name=&quot;ond_id&quot; type=&quot;checkbox&quot;
<?php
	if (in_array ($myrow, $checked_values))
	{
	echo &quot;checked &quot;;
	}
?>

value=&quot;<?php echo $myrow[&quot;ond_id&quot;] ?>&quot;> <?php echo $myrow[&quot;titel&quot;] ?> 
<?php
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top