form/submit, how to check if array ISSET()

I have a form that has multiple check boxes and if the visitor does not select any of them the form is reloaded for them to make a manitory selection of at least one.

here is a snippet of the code… (yes i know i have inline css, these will be changed to classes once it is finished)

<div style="float: left;<? if (isset($sectors) && empty($sectors)) { echo(' border: 1px solid #FF4040;'); } ?>"><input type="checkbox" name="sectors[]" value="select1"<?
if (isset($sectors) && in_array("select1", $sectors)) { echo(" checked"); } ?>></div><div style="float: left;"> select1
</div><br class="clear">

<div style="float: left;<? if (isset($sectors) && empty($sectors)) { echo(' border: 1px solid #FF4040;'); } ?>"><input type="checkbox" name="sectors[]" value="select2"<?
if (isset($sectors) && in_array("select2", $sectors)) { echo(" checked"); } ?>></div><div style="float: left;"> select2
</div><br class="clear">

When the page reloads if shows a border around each checkbox so they know to check at least one of them as the message above the form will tell them.

What I have a problem with is getting the script to know if the array is set or not. When the page loads first time around nothing is set so not red borders are shown, when the form is submitted then all strings are set and if any are empty then the border is shown to let them know that it much be filled in. But how do I find out if an array ISSET ?

The code above all of this creates the strings for each field, like this for the sectors…

$sectors = $_POST['sectors'];

For some reason the ISSET part is not knowing if the array is set or not.

if i take out the… isset($sectors) && then the page shows the red border all the time even on the first load, which is not what I need.

<div style="float: left;<? if (	empty($sectors)	) { echo(' border: 1px solid #FF4040;'); } ?>"><input type="checkbox" name="sectors[]" value="select1"<?
if (isset($sectors) && in_array("select1", $sectors)) { echo(" checked"); } ?>></div><div style="float: left;"> select1
</div><br class="clear">

Check for isset($_POST) before starting your form processing.

Sponsor our Newsletter | Privacy Policy | Terms of Service