checkbox selection & arrays
<p>I have 9 check boxes in my select.php</p>

<pre><code>&lt;form method="post" action="test.php"&gt;
&lt;input type="checkbox" name="g1[]" id="c1" value="c1"&gt;
&lt;input type="checkbox" name="g1[]" id="c2" value="c2"&gt;
&lt;input type="checkbox" name="g1[]" id="c3" value="c3"&gt;

&lt;input type="checkbox" name="g2[]" id="h1" value="h1"&gt;
&lt;input type="checkbox" name="g2[]" id="h2" value="h2"&gt;
&lt;input type="checkbox" name="g2[]" id="h3" value="h3"&gt;

&lt;input type="checkbox" name="g3[]" id="d1" value="d1"&gt;
&lt;input type="checkbox" name="g3[]" id="d2" value="d2"&gt;
&lt;input type="checkbox" name="g3[]" id="d3" value="d3"&gt;
&lt;/form&gt;
</code></pre>

<p>ok my test.php looks like this now:</p>

<pre><code>error_reporting(E_ALL);

$g1 = $_POST['g1']; 
$g2 = $_POST['g2']; 
$g3 = $_POST['g3'];

//Connect to DB

$ng1 = count($g1);
$ng2 = count($g2);
$ng3 = count($g3);

 $sum = 0; 
 for ($i = 1; $i &lt;= 3; $i++) 
 { 
     $arr = "g$i"; 
     if (!isset($_POST[$arr])) 
         ${$arr} = array(); 
     else 
         ${$arr} = $_POST[$arr]; 
     if (!is_array(${$arr})) 
         die("Error in input parameter $arr"); 
     ${"ng$i"} = count(${$arr}); 
     if (${"ng$i"} &lt; 1) 
         die("At least one $arr checkbox must be checked"); 
     $sum += ${"ng$i"}; 

     ${"g$i"."_sql"} = implode(',', array_map(${$arr}, 'mysql_real_escape')); 
 } 

 $query="INSERT INTO ch_lg (g1, g2, g3) VALUES ('$g1_sql','$g2_sql','$g3_sql')"; 

 mysql_query($query) or die(mysql_error()); 
 mysql_close(); 

//echo message
}
</code></pre>

<p><strong>Modifid Question</strong></p>

<p>I need to check that:</p>

<ol>
<li><p>The user chose 1 checkbox from each array (your code does this)</p></li>
<li><p>He didn't select more than 4 checkboxes (your code does this)</p></li>
<li><p>He chose 1 more checkbox from JUST one array (meaning that he has to chose 4 in total and that just 1 of each is not acceptable) - I think this is not happening in your code. Does it?</p></li>
</ol>

<p>Thank you</p>
