Web Technologies - Old Questions

13.  Explain the form handling with example.

5 marks | Asked in 2068

Handling forms is a multipart process. First a form is created, into which a user can enter the required details. This data is then sent to the web server, where it is interpreted, often with some error checking.

PHP Form Handling with POST

If we specify the form method to be POST, then the form-data is sent to the server using the HTTP POST method.

syntax:

<?php

 $_POST['variable_name'];

?>

E.g. 

//form1.html

<form action="login.php" method="post">   

<table>   

<tr><td>Name:</td><td> <input type="text" name="name"/></td></tr>  

<tr><td>Password:</td><td> <input type="password" name="password"/></td></tr>   

<tr><td colspan="2"><input type="submit" value="login"/>  </td></tr>  

</table>  

</form>   

//login.php

<?php  

$name=$_POST["name"];   //receiving name field value in $name variable  

$password=$_POST["password"];   //receiving password field value in $password variable  

echo "Welcome: $name, your password is: $password";  

?>  

PHP Form Handling with GET

If we specify the form method to be GET, then the form-data is sent to the server using the HTTP GET method.

syntax:

<?php

$_GET['variable_name'];

?>

E.g. 

//form1.html

<form action="welcome.php" method="get">  

    Name: <input type="text" name="name"/>  

    <input type="submit" value="visit"/>  

</form>  

//welcome.php

<?php  

$name=$_GET["name"];    //receiving name field value in $name variable  

echo "Welcome, $name";  

?>