Question

Ok I am new to programming and I have an issue with some checkboxes.
I want to store if a check box is checked and display this when button is pressed.
I am making a filter search, so when a person selects a check box it will only display that filter.
However it only works if I submit and select or deselect a checkbox twice.
How can I make it to only have to do this once?

<form name="search" action="<? $_SERVER['PHP_SELF']; ?>" method="post">
   <input type="checkbox" name="checkAllMyCB" id="checkAllMyCB" onclick="jqCheckAll2( this.id, 'myCB' )"/>All
   <input name="category[]" type="checkbox" <? if (in_array("art", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="art">Art
   <input name="category[]" type="checkbox" <? if (in_array("church", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="church">Church
   <input name="category[]" type="checkbox" <? if (in_array("education", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="education">Education/Schools
   <input name="category[]" type="checkbox" <? if (in_array("food", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="food">Food
   <input name="category[]" type="checkbox" <? if (in_array("gardening", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="gardening">Gardening
   <input name="category[]" type="checkbox" <? if (in_array("kids", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="kids">Kids
   <input name="category[]" type="checkbox" <? if (in_array("music", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="music">Music
   <input name="category[]" type="checkbox" <? if (in_array("outdoors", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="outdoors">Outdoors
   <input name="category[]" type="checkbox" <? if (in_array("seniors", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="seniors">Seniors
   <input name="category[]" type="checkbox" <? if (in_array("sports", $_SESSION['category'])) { echo "checked='checked'";  } ?> value="sports">Sports/Physical Activity
   <input name="category[]" type="checkbox" <? if (in_array("support", $_SESSION['category'])) { echo "checked='checked'";  } ?>value="support">Club/Support Group
   <input type="submit" name="Go2" id="Go2" value="Go &gt;" />
<? 
$_SESSION['category'] = $_POST['category']; ?>

</form>
Was it helpful?

Solution

Two things are wrong in your code:

  1. 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.

  2. You are not checking whether the SESSION variable has been set before the in_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 &gt;" />
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top