Two things are wrong in your code:
You are first displaying the form and then setting values into the
SESSION
. This should be the opposite way because when submitted, your form would be displayed first and only after the values would get set which is why you have the problem of submitting it twice to see the values.You are not checking whether the
SESSION
variable has been set before thein_array
, this would throw an error initially. The same case with$_POST['category']
First place this on top:
<?
if (isset($_POST['category'])){
$_SESSION['category'] = $_POST['category']; }
else
{
unset($_SESSION['category']);
}
?>
Then change your form code to:
<form name="search" action="" method="post">
<input type="checkbox" name="checkAllMyCB" id="checkAllMyCB" onclick="jqCheckAll2( this.id, 'myCB' )"/>All
<input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("art", $_SESSION['category'])) { echo "checked='checked'"; } ?> value="art">Art
<input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("church", $_SESSION['category'])) { echo "checked='checked'"; } ?> value="church">Church
<input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("education", $_SESSION['category'])) { echo "checked='checked'"; } ?> value="education">Education/Schools
<input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("food", $_SESSION['category'])) { echo "checked='checked'"; } ?> value="food">Food
<input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("gardening", $_SESSION['category'])) { echo "checked='checked'"; } ?>value="gardening">Gardening
<input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("kids", $_SESSION['category'])) { echo "checked='checked'"; } ?> value="kids">Kids
<input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("music", $_SESSION['category'])) { echo "checked='checked'"; } ?> value="music">Music
<input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("outdoors", $_SESSION['category'])) { echo "checked='checked'"; } ?> value="outdoors">Outdoors
<input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("seniors", $_SESSION['category'])) { echo "checked='checked'"; } ?> value="seniors">Seniors
<input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("sports", $_SESSION['category'])) { echo "checked='checked'"; } ?> value="sports">Sports/Physical Activity
<input name="category[]" type="checkbox" <?php if (isset($_SESSION['category']) && in_array("support", $_SESSION['category'])) { echo "checked='checked'"; } ?> value="support">Club/Support Group
<input type="submit" name="Go2" id="Go2" value="Go >" />
</form>