Day 7: Form Handling and Types of errors in PHP

Form is one of the powerful features of PHP. Below is an example of HTML Form:

Program 25: Form example with GET method.

<?php
echo “hello “.$_GET[‘txtName’];
?>
<html>
<body>

<form>
Name: <input type=”text” name=” txtName”> <br>
<input type=”submit”>
</form>

</body>
</html>


Note: By default form has get method, so $_GET is use in above example. In php “name” attribute of html controls (like textbox, checkbox, select, etc.) is use to access its value. Like “txtName” is used in above example.


Program 26: Form example with POST method.

<?php
if(isset($_POST[‘btnSum’])) {
$num1 = $_POST[‘txtNum1’];
$num2 = $_POST[‘txtNum2’];
$res = $num1+$num2;
}
?>
<html>
<body>
<form method=”POST”>
First Number: <input type=”text” name=”txtNum1″> <br>
Sec. Number: <input type=”text” name=”txtNum2″> <br>
Result : <input type=”text” name=”txtResult” value=”<?php echo $res; ?>”> <br>
<input type=”submit” value=”sum” name=”btnSum”>
</form>
</body>
</html>

Note: isset() function is use to check is button (i.e “btnSum”) clicked or not. It will return true if button (name passed as parameter) is clicked and false if not clicked. With the help of isset() function we can also handle clicks of more than one button.

If we are using “POST” method in html form, so we have to use $_POST in php, $_GET will not work in this case.


Program 27: Form example with REQUEST method.

<?php
if(isset($_REQUEST[‘btnSum’])) {
$num1 = $_REQUEST[‘txtNum1’];
$num2 = $_REQUEST[‘txtNum2’];
$res = $num1+$num2;
}
if(isset($_REQUEST[‘btnMul’])) {
$num1 = $_REQUEST[‘txtNum1’];
$num2 = $_REQUEST[‘txtNum2’];
$res = $num1*$num2;
}
?>
<html>
<body>
<form method=”POST”>
First Number: <input type=”text” name=”txtNum1″> <br>
Sec. Number: <input type=”text” name=”txtNum2″> <br>
Result : <input type=”text” name=”txtResult” value=”<?php echo $res; ?>”> <br>
<input type=”submit” value=”sum” name=”btnSum”>
<input type=”submit” value=”mul” name=”btnMul”>
</form>
</body>
</html>

Note: $_REQUEST can be use with both GET and POST method of html form. In above example you can change form method GET and will see same output.


Program 28: Form with other HTML controls.

<?php
if(isset($_REQUEST[‘btnSubmit’])) {
echo “<h2>Entered data is </h2>”;
echo “Name : “.$_REQUEST[‘txtName’];
echo “<br>City : “.$_REQUEST[‘drpCity’];
echo “<br>Gender : “.$_REQUEST[‘rdGener’];
echo “<br>Address : “.$_REQUEST[‘txtAddress’];
echo “<hr>”;
}
?>
<html>
<body>
<form method=”POST”>
Name: <input type=”text” name=”txtName”> <br>
City: <select name=”drpCity”>
<option value=””>Select</option>
<option value=”Delhi”>Delhi</option>
<option value=”Chandigarh”>Chandigarh</option>
</select> <br>
Gender: <input type=”radio” name=”rdGener” value=”male”/> Male
<input type=”radio” name=”rdGener” value=”female”/> FeMale
<br>
Address: <textarea rows=”4″ cols=”20″ name=”txtAddress”></textarea> <br>
<input type=”submit” value=”SUBMIT” name=”btnSubmit”>
</form>
</body>
</html>

Types of errors in PHP

There are three basic types of runtime errors in PHP:

  1. Notices: Non-critical errors which PHP encounters while running a script.
    Example: A variable accessed before it is declared.
    Error message will show but program will not terminate.
  2. Warnings: The are more serious errors. For example, A number is divided by Zero (0).

    Error message will show but program will not terminate.

  3. Fatal Errors: These errors are critical errors. For example, creating an object of a non-existent class.

    These errors terminate the script’s execution immediately. These are intimated to the users.

Leave a Reply