Forms
• Form processing is a very important feature of PHP.
• It takes users input and generate subsequent pages based on the user input.
• There are two ways the browser client can send information to the web server.
• The GET Method
• The POST Method
GET Method
• The GET method produces a long string that appears in your server
logs, in the browser's Location: box.
• The GET method is restricted to send up to 1024 characters only.
• Never use GET method if you have password or other sensitive
information to be sent to the server.
• GET can't be used to send binary data, like images or word documents,
to the server.
• The data sent by GET method can be accessed using QUERY_STRING
environment variable.
• The PHP provides $_GET associative array to access all the sent information
using GET method.
Example:
[Link]
<?php
if( $_GET["name"] || $_GET["age"] )
{
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old."; exit();
}
?>
<html> <body>
<form action="<? php $_PHP_SELF ?>" method="GET">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body> </html>
POST Method
• The POST method does not have any restriction on data size to be sent.
• The POST method can be used to send ASCII as well as binary data.
• The data sent by POST method goes through HTTP header so security
depends on HTTP protocol. By using Secure HTTP you can make sure that
your information is secure.
• The PHP provides $_POST associative array to access all the sent
information using POST method.
<?php
if( $_POST["name"] || $_POST["age"] )
{
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['age']. " years old.";
exit();
} ?>
<html> <body>
<form action="<?php $_PHP_SELF ?>" method="POST">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form></body>
</html>
Form Validation
•To validate the data being entered by the user.
• Many in-built functions are there.
is_string(string) – Determine whether a variable is string. Return a true or false value.
is_int(string) – Determine whether a variable is integer. Returns a true or false
value.
is_float(string) - Determine whether a variable is a float. Returns a true or false
value.
strlen(string) – Returns the length of a string.
strtolower(string) – Converts the string into lowercase.
strtoupper(string) – Converts the string into uppercase.
isset(string) - Check whether the variable declared.
empty(string) - Check whether the variable is empty.
trim(string) - Used to trim the extra spaces.
Sample Program for
Validation
<?php
if(isset($_POST['Submit']))
{
$name=trim($_POST["name"]);
$number=trim($_POST["number"]);
$email=trim($_POST["email"]);
if($name == "" )
{
$error= "error : You did not enter a name.";
$code= "1" ;
}
elseif($number == "" )
{
$error= "error : Please enter number.";
$code= "2";
}
//check if the number field is numeric
elseif(is_numeric(trim($_POST["number"])) == false )
{
$error= "error : Please enter numeric value.";
$code= "2";
}
elseif(strlen($number)<5)
{
$error= "error : Number should be five digits.";
$code= "2";
}
//check if email field is empty
elseif($email == "" ) {
$error= "error : You did not enter a email.";
$code= "3";
}
//check for valid email
elseif(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-
Z]{2,6}$/i", $email))
{
$error= 'error : You did not enter a valid email.';
$code= "3";
}
else{
echo "Success";
//final code will execute here.
}
}
?>