Jump to content

PHP - Posting checkbox yes/no to form


lrintoul

Recommended Posts

I'm working on an online application form with a few checkboxes. They are yes or no answers. The form information is sent via email to the company for review.

 

This is part of the code from the form:

 

Hours of Experience: <input name="hours" id="hours" class="info" type="text" size="15" value="<?php echo (isset ($_POST['hours'])? $_POST['hours'] : "");?>"/>

Do you have a current Driver's License?
Yes 
<input name="license_yes" id="license_yes" type="checkbox" size="10" value="<?php echo (isset ($_POST['license_yes'])? $_POST['license_yes'] :"");?>"/>
No
<input name="license_no" id="license_no" type="checkbox" size="10" value="<?php echo (isset ($_POST['license_no'])? $_POST['license_no'] : "");?>""/></td>

 

This is code to be posted in the form email. I can get the general text info to post but I don't know how to post the checkbox answers.

 

Hours of Experience: '.$_POST["hours"].'

 

I'd really appreciate some help.  Thanks.

Link to comment
https://forums.phpfreaks.com/topic/207868-php-posting-checkbox-yesno-to-form/
Share on other sites

I would actually make those check boxes, radio buttons and name them the same thing -- this way only one of them can be selected at a time and you only have to look at one post variable instead of a yes and no variable.

 

then you would access it just like everything else via $_POST

They are only sent to the post if they are checked for checked boxes. Thus do a simple isset() check... if not set then it was not sent and thus not checked.

 

if(isset($_POST['license_yes'])){
//It's checked do something with it.
}

if(isset($_POST['license_no'])){
//It's checked do something with it.
}

 

But of course I agree with radar if this a yes / no question then it needs to be radio buttons so they can't both be checked.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.