HTML Old School

Input checkbox

Checkbox is used when we want to select more items.

<form action="" method="POST" enctype="application/x-www-form-urlencoded">
 I want to buy:
 <br><input type="checkbox" name="fruit1" value="apples"> APPLES
 <br><input type="checkbox" name="fruit2" value="oranges" checked> ORANGES
 <br><input type="checkbox" name="fruit3" value="bananas"> BANANAS
</form>

 

Example: 05checkbox.html

 

 Checkbox attributes

1. name="string"

 

 

2. value="string"

 

 

3. checked

Defines checked checkbox by default.

 

 

Sending variables to the server

 When we send checkbox variables to the server we are sending several independant variables.

For example: fruit1, fruit2, fruit3. If fruit1 and fruit3 are not checked then variables will not exist in PHP script. In that case use isset() PHP function.

Form with 3 checkboxes: 05checkbox_form.html

PHP script: 05checkbox_script.php

 

 

 Another way is to use the array as input field name: fruit[] .

<form action="" method="POST" enctype="application/x-www-form-urlencoded">
 I want to buy:
 <br><input type="checkbox" name="fruit[]" value="apples"> APPLES
 <br><input type="checkbox" name="fruit[]" value="oranges" checked> ORANGES
 <br><input type="checkbox" name="fruit[]" value="bananas"> BANANAS
</form>

 Form with array checkbox variables: 05checkbox_arr_form.html

PHP script with foreach(): 05checkbox_arr_script.php