Form Handing

advertisement
PHP FORM HANDLING
HTML Form

Forms are used to collect user data and pass data to a server via different input fields.

All input controls are placed in between <form> and </form>.

Syntax
<form
action=“PageToOpen”
method=“ ”>
//input fields
</form>

Action attribute is used to defines the action to be performed.
<form action="/action_page.php" >

Method attribute is used to defines HTTP method (Get and Post) to collect and submit form
data to server or any other page.
<form action="/action_page.php" method=“get”>
HTML FORM
<form action="welcome.php" method=“GET">
First Name <input type="text" name="fname"><br><br>
Last Name <input type="text" name="lname"> <br><br>
<input type="submit" name="button" value="Click It">
</form>
HTML FORM
<html>
<body>
Welcome <?php echo "$_GET['fname'] $_GET['lname']"; ?>
</body>
</html>
Input Fields

Different inputs are available in a form that are used to get input from a user.

Text fields, text areas, selection lists, and radio buttons, etc.

When the user clicks the “submit” button, each of these above becomes a string of text offered
to the Web server.

When a form is submitted, PHP turns each form field into a variable.
Get and Post Methods

A form data can be submitted using POST and GET method.

Both are used for same purpose but stand apart for some specification.

GET and POST create an array which holds key/value pairs, where keys are the name of
the form controls and values are the input data by the user.
array( key => value, key2 => value2, key3 => value3, ...)

Both GET and POST method are treated as $_GET and $_POST in PHP.

These methods are superglobals, which means that they are always accessible, and
they can be accessed using any function, class or file.
When to Use GET ?

Information sent from a form with the GET method is visible to everyone.

All variables names and values displayed in the URL.

It sends limited data up to 2000 characters.

Never use GET when a sensitive data needs to send.

A form with GET can be bookmarked.

Dos not support for binary data.

Best used for non-secure data like query strings.
When to Use POST ?

With POST method sent information is invisible to others.

All variables/values are embedded within the body of HTTP request method.
No limits on the length of information to be sent.
Supports for both ASCI and Binary information.
It is not possible to bookmark the page.
Used to send secure information like credit card numbers, passwords or information that is to
be stored in a database, to the server.




Accessing Form Variables

$_GET, $_POST, $_REQUEST

PHP super global associative arrays.

Hold information sent from an HTML form.

PHP automatically places each form variable value into:



$_GET[form variable name]
$_POST[form variable name]
$_REQUEST[form variable name]
Accessing Form Variables
$_GET
Hold information sent from an HTML form using GET method:
$_GET['var’]
 $_POST
Hold information sent from an HTML form using POST method:
$_POST['var’]
 $_REQUEST
The form data submitted using either POST or GET method, are also available in $_REQUEST
variables.

Example: GET Method
<body>
<h1>Sum of Tow Numbers</h1>
<form action="" method=“GET">
First Number <input type="text" name="firstNum"><br><br>
Second Numnber <input type="text" name="secondNum"> <br><br>
<input type="submit" name="button" value="Calculate" width="100px">
</form>
<?php
if($_GET['firstNum'] !=" " && $_GET['secondNum'] !=" ")
{
$result=$_GET['firstNum'] + $_GET['secondNum'];
echo "<br>Result=$result";
}
?>
</body>
Output
Example: POST Method
<body>
<h1>Sum of Tow Numbers</h1>
<form action="" method=“POST">
First Number <input type="text" name="firstNum"><br><br>
Second Numnber <input type="text" name="secondNum"> <br><br>
<input type="submit" name="button" value="Calculate" width="100px">
</form>
<?php
if($_POST['firstNum'] !=" " && $_POST['secondNum'] !=" ")
{
$result=$_POST['firstNum'] + $_POST['secondNum'];
echo "<br>Result=$result";
}
?>
</body>
Output
Complete Admission Form using POST Method
ANY QUESTION…?
THANKS
FOR
YOUR
CONCENTRATION
Related documents
Download