PHP Multiple checkbox

I’m trying to filter a portfolio with multiple checkboxes

I can’t get the checkbox corresponding to each subcategory in an array to be selected by default.

Also making filter selections with multiple checkboxes doesn’t seem to work, when I click on a checkbox the page refreshes and the item in that subcategory gets dispalyed, which is correct, however I can’t make it work to change the array so I can filter with multiple checkboxes since it refreshes the moment the checkbox gets clicked.

And the checkboxes always change to their default state so you can’t see which is selected or not

<div id="portfolio">
<h1>Portfolio</h1>
<div id="gallery">
<?php

include('beheer/includes/config.inc.php');

	$stage_width=600;
	$stage_height=600;

	$i=1;
	if(is_array($_POST['cat'])){
	$toon = implode(',', $_POST['cat']);
	
	$query = "SELECT * FROM foto WHERE subcategorieID IN(".$toon.")";
	}else{
	$query = "SELECT * FROM foto";	
	}
	
	$result = mysql_query($query) or die(mysql_error());
	while($row = mysql_fetch_array($result)){

		$left=rand(0,$stage_width);
		$top=rand(0,300);
		$rot = rand(-40,40);
			
		echo '
		<div id="pic-'.($i++).'" class="pic" style="top:'.$top.'px;left:'.$left.'px;background:url(photos/thumbs/'.$row['fotoID'].'.jpg) no-repeat 50% 50%; -moz-transform:rotate('.$rot.'deg); -webkit-transform:rotate('.$rot.'deg);">
		<a class="fancybox" rel="fncbx" href="photos/original/'.$row['fotoID'].'.jpg" target="_blank">'.$row['fotoTitel'].'</a>
		</div>';
	}
   ?>
</div>
</div>
<div id="filter">
<h1>Categorie&euml;n</h1>
<form method="POST">
<?php
$i=1;
$query =	'SELECT 
			categorie.categorieID as categorieID,
			categorie.categorieNaam as categorieNaam,
			subcategorie.subcategorieID as subcategorieID,
			subcategorie.subcategorieNaam as subcategorieNaam
			FROM categorie, subcategorie
			WHERE categorie.categorieID = subcategorie.categorieID';
$result = mysql_query($query) or die(mysql_error());
	while($row = mysql_fetch_array($result)){
		if($row['categorieID'] != $oudID){
			if($oudID != ''){
			echo '</p>
';	
	}
			echo '<h3>'.$row['categorieNaam'].'</h3>
';
			echo '<p class="over">
';
			
			$oudID = $row['categorieID'];
		}
		
		if(!is_array($_POST['cat']) || in_array($row['subcategorieID'],$_POST['cat'])){
                $selected = 'selected="selected"';     
        }else{
                $selected = '';
        }
		echo '<input name="cat[]" type="checkbox" value="'.$row['subcategorieID'].'" '.$selected.' onclick="this.form.submit();" /> '.$row['subcategorieNaam'].'<br />
';
		}
echo '</p>
';		
?>
</form>
</div>

Consider using a form action with specific case events.

Example:
[php]switch($_POST[‘action’]){
case ‘Submit’:
//Your code here
//What should happen when the submit button is pressed
//Your code here

break;
}[/php]

[code]
{…code…}

[/code]

You currently have your code to submit the form when ever the user ticks a box. (Line 67)
[php]echo ‘<input name=“cat[]” type=“checkbox” value="’.$row[‘subcategorieID’].’" ‘.$selected.’ onclick=“this.form.submit();” /> ‘.$row[‘subcategorieNaam’].’
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service