A check box is another control that, like a text field, is created in HTML using the <input> element. It provides a single box that can be ticked or not, depending on the option chosen. It doesn't require any data from the user, other than a click in the check box, so any data this control contains is quite different from the text field. In HTML it looks very similarâ€”the only difference is the type:
<input name="Choice" type="checkbox">
A check box is appropriate when you have a question that requires a strict yes/no answer. Check boxes also have a checked attribute, which takes no value. Supply this attribute in the control and the check box will be checked by default:
<input name="Choice" type="checkbox" checked>
The control also has a value attribute set to on by default.
Some of the advantages of check boxes over other forms of input control aren't immediately obvious, but they become clear after you've used several of them. The following exercise utilizes just one check box and returns the contents of it to the screen.
Try it Out: Use a Check Box
Again, there's no query string on the end of the URL because you used the POST method to pass your form information to the Web server. You set this in the first of the two programs, checkbox.html:
The check box form control is created with the <input> element:
Have you ever eaten haggis before? <input name="Choice" type="checkbox">
In checkbox.php, you call the PHP variable, which has exactly the same name as the control you set in checkbox.html:
<?php echo $_POST['Choice']; ?>
The only difference is that the variable is now created with a value that wasn't assigned by you. If the check box was ticked by the user, then it contains the value on. If it wasn't, then it contains nothing at all.
|copyright © dedserveronline.com|