Hello all...I have been struggling with this for hours now and I have read several posts and have gotten myself all turned around. Help would be appreciated. These are small snippets of the code. The problem is with avalable_days[] checkboxes:

//I initiate variables for the form (which is very large ...over 140 fields)

if (isset($hour_sal_amt)){
$hour_sal_amt=$hour_sal_amt;
} else $hour_sal_amt="";
if (isset['available_days']) {
$available_days=$available_days;
} else $available_days="";
if (isset($avail_startdate)) {
$avail_startdate=$avail_startdate;
} else $avail_startdate="";
if (isset($shifts)){
$shifts=$shifts;

//validation

	if(!isset($_POST['available_days'])) $errors.="• What days of the week are you available to work?<br>";

	if(strlen($errors)>0)
	{ 
	$pageno=2; 
	include("errorbox.inc"); 
		}

//then I have the form fields

	    <input name="available_days[M]" type="checkbox" <? if (isset($_POST['available_days']['M'] )){echo "checked";}?>>
        M 
        <input name="available_days[T]" type="checkbox" <? if (isset($_POST['available_days']['T'] )){echo "checked";}?>>
        T 
        <input name="available_days[W]" type="checkbox" <? if (isset($_POST['available_days']['W'] )){echo "checked";}?>>
        W 
        <input name="available_days[Th]" type="checkbox" <? if (isset($_POST['available_days']['Th'] )){echo "checked";}?>>
        Th 
        <input name="available_days[F]" type="checkbox" <? if (isset($_POST['available_days']['F'] )){echo "checked";}?>>
        F 
        <input name="available_days[Sat]" type="checkbox" <? if (isset($_POST['available_days']['Sat'] )){echo "checked";}?>>
        Sat 
        <input name="available_days[Sun]" type="checkbox" <? if (isset($_POST['available_days']['Sun'] )){echo "checked";}?>>
        Sun 

The error message pops up even if something had been checked and the checked checkboxes do not remained checked. Do I need to loop? I am lost.

Thanks

    available_days is an array, right?
    and the error you speak of, I'm guessing you mean this
    if(!isset($_POST['available_days'])) $errors.="• What days of the week are you available to work?<br>";

    Since it's an array, you should either do a count or look for the first value.

    if(count($_POST['available_days']) == 0) {
       $errors.="• What days of the week are you available to work?<br>";
    }
    
    // OR
    if(!isset($_POST['available_days'][0])) {
       $errors.="• What days of the week are you available to work?<br>";
    }

      OK Got the validation part to work. This is the problem:

      initializing $available_days[]:

      for ($i = 0; $i < count($available_days); $i++) {
      if (isset($available_days[$i])) {
      $day[$i] = "on";
      }
      else
      {
      $day[$i] = "";
      }
      }

      trying to set the selected checkbox to checked:

      <input name="available_days[]" type="checkbox" value="1" <? if ($day1 !=" "){echo "checked";}?>>M
      <input name="available_days[]" type="checkbox" value="2" <? if ($day2 !=" "){echo "checked";}?>>T
      <input name="available_days[]" type="checkbox" value="3" <? if ($day3 !=" "){echo "checked";}?>>W
      etc....

      The validation I have now works but how can I get the page to realize that the checkbox has been selected if i need to resubmit due to another error on the page. I dont want the page to wipe out the selction.

        Write a Reply...