Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

PHP

Learn step by step



PHP 5 Form Handling

The PHP superglobals $_GET and $_POST are used to collect form-data.

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>
The GET Method

The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character.

http://www.orianaitsulutions.com/index.htm?name1=value1&name2=value2

  • 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 upto 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.

<?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>
The POST Method

The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.

  • 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>
The $_REQUEST variable

$_REQUEST is a super global variable which is widely used to collect data after submitting html forms.

<html>
<head>
<title>Php contact form</title>
</head>
<body>
<form name="contact" method="post" action="contact.php">
<table bgcolor=cornsilk style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0" align="center" >
<tr><td>&nbsp;</td><td>
&nbsp;</td></tr>
<tr><td style="font-family: verdana; font-size: 10pt" align="right">
<b><font color=#CC0000>*</font><font color=#004080> Name:</font></b></td><td>
<font color="#006600"><b><input size=25 name="Name"></b></font></td></tr>
<tr><td style="font-family: verdana; font-size: 10pt" align="right"><b><font color=#CC0000>*</font><font color=#004080> Email:</font></b></td><td>
<font color="#006600"><b><input size=25 name="Email"></b></font></td></tr><br>
<tr><td style="font-family: verdana; font-size: 10pt" align="right"><b><font color="#004080">Address:</font></b></td><td align="right">
<font color="#006600"><b><input size=25 name="Company" style="float: left"></b></font></td></tr>
<tr><td style="font-family: verdana; font-size: 10pt" align="right">
<b><font color="#004080">Contact No:</font></b></td><td>
<font color="#006600"><b><input size=25 name="Phone"></b></font></td></tr>
<tr><td colspan=2 style="font-family: verdana; font-size: 10pt">
<font color="#004080"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Message</b></font><b><font color="#006600">:</font></b></td></tr>
<tr><td colspan=2 align=center><textarea name="Message" rows=5 cols=35></textarea></td></tr>
<tr><td colspan=2 align=center><input class="formbutton" type=submit name="send" value="Submit"></td></tr>
<tr><td colspan=2 align=center style="font-family: verdana; font-size: 10pt"><small>A <font color=red>*</font> indicates a field is required</small></td></tr>
</table>
</form>
</body>
</html>

<?php
$name=$_REQUEST['name'];
echo $name;
?>


Related Videos