Reading Data in a form: using check box, lesson -4-…
his week I will talk about handling check box. They are those square controls that you can select or de-select in a form. So, let’s begin…
Step *1: open xammp control panal, open your browser and open the editor. In htdocs folder which is usually be in (C:\xampp\htdocs). in this folder create another folder and name it (checkbox). Inside this folder you will save the files we will create in this lesson. In this folder we will have two files checkbox.html and phpcheckbox.php.
Step *2: using the editor, create an HTML file, name it checkbox.html, type this code in it and save.
<htm> <head> <title> Entering data into check box </title> </head> <body> <h1> Entering data into check box </h1> <form method="post" action="phpcheckbox.php"> Do you love reading books? <input value="yes"> Yes <input value="no"> No <br/> <br/> <input value="send"> </form> </body> </html>
As I said before we are using post method because it is more secure to send data. And in action, we specify the file name that data been sent to.
For handling check box, we should use the attribute “input”. named them, by the attribute “name”. specify the input type, which is here “checkbox”. And give them a value either yes or no, by the attribute “value”.
At last adding another input button which is submit button to send the data.
Step *3: now, reading data in a web page that are been sent by the check box.
You may think, you could do that by typing the information stored in $_request like before. Unfortunately, this is not right. Why?
Because the user may not have checked a check box. Therefore trying to display the data in check box will give you an error message.
The solution is by checking up if there is any data in a particular check box before typing them. You can check up if an array has an element with a certain index with the function (isset). Here is how that works.
Create a PHP file and name it phpcheckbox.php. type this code in it and save.
<html>
<head>
<title>
reading data from check box
</title>
</head>
<body>
<h1> reading data from check box </h1>
you selected:
<?php
if (isset ($_REQUEST["check1"])) {
echo $_REQUEST["check1"], "<br/>";
}
if (isset($_REQUEST["check2"])) {
echo $_REQUEST["check2"], "<br/>";
}
?>
</body>
</html>
As you can see, the isset function checks if there is a value in each check box before typing the data stored in it. This way we will avoid the error message and we learned the benefit of a new function in php.
Note that the user may check both of the boxes ( yes and no) which will be confusing. So, to force the user to check only one box we should use radio buttons. And this what I will gonna talk about next week. Seeyaz.








Leave a Reply