Php FORM (checkbox control)

To analyze this control we will use practically the same example as with the radio object:
Implement a form that requests the loading of two integers, one in each text. Have two checkbox-type controls that allow us to select whether we want to add and / or subtract the entered values.

The html form has the following code:

<head>
<title> Problem </title>
</head>
<body>
<form action = “page2.php”
method = “post”>
Enter first value:
<input type = “text” name = “value1”>
<br>
Enter second value:
<input type = “text” name = “value2”>
<br>
<input type = “checkbox” name = “check1”> add
<br>
<input type = “checkbox” name = “check2”> subtract
<br>
<input type = “submit” name = “operate”>
</form>
</body>
</html>

New to this problem are the two checkbox controls:

<input type = “checkbox” name = “check1”> add
<br>
<input type = “checkbox” name = “check2”> subtract
<br>

It is important to note that each checkbox has a different name.

Now let’s look at the code of the page that processes the form:

<html>
<head>
<title> Problem </title>
</head>
<body>
<? php
if (isset ($_ REQUEST [‘check1’]))
{
$sum = $_ REQUEST [‘value1’] + $_REQUEST [‘value2’];
echo “The sum is:”. $sum. “<br>”;
}
if (isset ($_ REQUEST [‘check2’]))
{
$subtraction = $_ REQUEST [‘value1’] – $_REQUEST [‘value2’];
echo “The subtraction is:”. $subtraction;
}
?>
</body>
</html>

If the checkbox is not selected in the form, an entry is not created in the associative vector $_REQUEST, to find out if a certain component exists in a vector, the isset function is used, if it returns true it means that it exists and therefore the checkbox is selected.
We have two if at the same height since the two controls of type checkbox could be selected.

This article was written by admin

Leave a Reply