PHP Form Handling notes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

PHP Form Handling

Creating a form on the webpage is accomplished using HTML, while


PHP serves as a transport for those values from the webpage to the
server and then in further processing those values.

PHP provides two superglobals $_GET and $_POST for collecting


form-data for processing.

Understanding How HTML Form Works

<html>
<body>

<form action="form-handler.php"
method="POST">
Name: <input type="text" name="name">
<br/>
Email: <input type="text"
name="email"> <br/>
<input type="submit">
</form>

</body>
</html>
In the code above, we have used the <form> tag to create an HTML
form, with input fields for Name and Email along with submit
button to submit the form-data.

In the <form> tag, we have two attributes, action and method,


action: Using this attribute, we can specify the name of the file which
will collect and handle the form-data. In the example above, we
have provided name of a Php file.

method: This attribute specify the means of sending the form-data,


whether it will be submitted via POST method or GET method.

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.
Below, we have the code, to access the form-data in the Php file
specified in the action attribute of our HTML form.

<?php

// getting the value of name field


$name = $_POST["name"];
// getting the value of the email field
$email = $_POST["email"];

echo "Hi, ". $name . "<br>";


echo "Your email address: ". $email ."<br>";

?>

Hi, epche

Your email address: we@epche.com

You will get the above output, if you provide name as "epche" and
email address as "we@epche.com".
Below we have the same form with method as GET,

<html>
<body>

<form action="form-handler.php"
method="GET">
Name: <input type="text" name="name">
<br/>
Email: <input type="text"
name="email"> <br/>
<input type="submit">
</form>

</body>

</html>

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.
Below, we have the code, to access the form-data in the Php file
specified in the action attribute of our HTML form, this time using the
GET superglobal.

<?php

// getting the value of name field


$name = $_GET["name"];
// getting the value of the email field
$email = $_GET["email"];

echo "Hi, ". $name . "<br>";


echo "Your email address: ". $email ."<br>";

?>
Hi, epche

Your email address: we@epche.com

GET vs. POST method

When a user submits a form, the values from the input fields are
stored in an array, like array(key1=>value1, key2=>value2,...) and then
passed on to the destination(Php file) specified in the action attribute
of the <form> tag.

Using GET method


In case of GET method, form-data is submitted as URL parameters,
i.e. all the values entered in the form fields by the user are sent to
the action script, appended in the URL.
As the form-data is visible to everyone because it sent as URL
parameters, hence we should not use GET method for a form
with sensitive data, like passwords etc.

Using POST method


When we use the POST method, the array of key-value pair(the
form-data), coming from the HTML form are sent as part of the
HTTP request, hence they are invisible to the user.
Also, there is no character limit for the information/data being
transmitted.
POST method also supports multipart form-data upload which is
used for file upload.
We would recommend, that you use the POST method while
working on any PHP web application/project.

You might also like