Ugh, I'm sure I'm doing something dumb. I've looked at several form posts about this same issue and I can't seem to figure out what I'm doing wrong. I keep getting Undefined Index when my checkboxes are unchecked and I've tried using isset to correct the problem, but I don't seem to be getting it.

My form is:

<input type="checkbox" name="templating" value="Yes" /> Templating
<p>
<input type="checkbox" name="del_fit" value="Yes" /> Delivery & Fitting
<p>
<input type="checkbox" name="wearhousepickup" value="Yes" /> Wearhouse Pickup
<p>
<input type="checkbox" name="delivery" value="Yes" /> Templating

and the PHP:

if(isset($_POST["templating"])){
	$templating="Yes";
	}

else{
	$templating="No";
	}


if(isset($_POST["del_fit"])){
	$del_fit="Yes";
	}

else{
	$del_fit="No";
	}


if(isset($_POST["wearhousepickup"])){
	$wearhousepickup="Yes";
	}

else{
	$wearhousepickup="No";
	}

if(isset($_POST["delivery"])){
	$delivery="Yes";
	}

else{
	$delivery="No";
	}

$templating = $_POST["templating"];
$del_fit = $_POST["del_fit"];
$wearhousepickup = $_POST["wearhousepickup"];
$delivery = $_POST["delivery"];

Can someone tell me what I'm doing wrong here?

Thanks!!

    Postjng from phone... Checkboxes do not show in the post array if the checkbox is unticked. That said you could change your yes value to 1 on your checkboxes and use something like:

    $var = $_POST[name] ? true : false;

    This is why your aren't seeing the checkbox post a "set". It will only recognize it if it is actually ticked. Checkboxes are a true false statement. It's either there or not. If it is in the post array then it's ticked otherwise it wasnt

      rigorlicious wrote:

      Can someone tell me what I'm doing wrong here?

      Yeah... you're doing the same thing twice, only the second time around you're doing it in a more error-prone way. 🙂

      Why do you have this:

      $templating = $_POST["templating"];
      $del_fit = $_POST["del_fit"];
      $wearhousepickup = $_POST["wearhousepickup"];
      $delivery = $_POST["delivery"];

      after the block if if() statements that defines those very same variables?

      As samuelcook points out, a more compact version of what you're doing can be achieved using the ternary operator (although I'd still suggest using [man]isset/man in the conditional part; otherwise you're going to be generating E_NOTICE level errors if the array index doesn't exist).

      EDIT: Also, when posting PHP or HTML code, please use the board's [noparse]

      ..

      or

      ..

      (respectively)[/noparse] bbcode tags as they make your code much easier to read and analyze.

        Write a Reply...