PHP Form Handling

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

PHP Form Handling

PHP - A Simple HTML Form


The example below displays a simple HTML form with two input fields and a submit
button:
<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>

When the user fills out the form above and clicks the submit button, the form data
is sent for processing to a PHP file named "welcome.php". The form data is sent
with the HTTP POST method.
To display the submitted data you could simply echo all the variables. The
"welcome.php" looks like this:
<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>


Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

The output could be something like this:

Welcome ABC
Your email address is ABC@example.com

………………………………………………………………………………………………………………
…………………………………………..
The same result could also be achieved using the HTTP GET method:

<html>
<body>

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


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

"welcome_get.php" looks like this:

<html>
<body>

Welcome <?php echo $_GET["name"]; ?><br>


Your email address is: <?php echo $_GET["email"]; ?>

</body>
</html>

…………………………………

GET vs. POST


Both GET and POST create an array (e.g. array( key1 => value1, key2 =>
value2, key3 => value3, ...)). This array holds key/value pairs, where keys are
the names of the form controls and values are the input data from the user.

Both GET and POST are treated as $_GET and $_POST. These are superglobals,
which means that they are always accessible, regardless of scope - and you can
access them from any function, class or file without having to do anything
special.

$_GET is an array of variables passed to the current script via the URL
parameters.

$_POST is an array of variables passed to the current script via the HTTP POST
method.
When to use GET?
Information sent from a form with the GET method is visible to everyone (all
variable names and values are displayed in the URL). GET also has limits on the
amount of information to send. The limitation is about 2000 characters.
However, because the variables are displayed in the URL, it is possible to
bookmark the page. This can be useful in some cases.

GET may be used for sending non-sensitive data.

Note: GET should NEVER be used for sending passwords or other sensitive
information!

http://localhost/prac/welcome_get.php?name=anurag&email=anurag.iet
%40gmail.com

When to use POST?


Information sent from a form with the POST method is invisible to others (all
names/values are embedded within the body of the HTTP request) and has no
limits on the amount of information to send.

Moreover POST supports advanced functionality such as support for multi-part


binary input while uploading files to server.

However, because the variables are not displayed in the URL, it is not possible
to bookmark the page.

Developers prefer POST for sending form data.

PHP Form Validation


<html>
<head>
<style>
.errorColor {color: #D30000;} // CSS class to display error message in red
</style>
</head>
<body>
<?php
// all required variables defined here
$nameError = $emailError = ""; // to collect type of error
$name = $email = $message = ""; // // to collect data

if ($_SERVER["REQUEST_METHOD"] == "POST") // check the type of method

{
if (empty($_POST["name"])) // checks whether name section is filled or not
{
$nameError = "Name is mandatory";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
$nameError = "Only letters allowed";
}
}

if (empty($_POST["email"])) {
$emailError = "Email is mandatory";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailError = "Invalid email format";
}
}

if (empty($_POST["message"])) {
$message = "";
} else {
$message = test_input($_POST["message"]);
}

function test_input($data) {
$data = trim($data); // Strip whitespace (or other characters) from the beginning and end of a string
$data = stripslashes($data); // the stripslashes() function removes backslashes
$data = htmlspecialchars($data); // Convert special characters to HTML entities
return $data;
}
?>

<h2><u>PHP Form With Validation</u></h2>


<p><span class="errorColor">* mandatory field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="errorColor">* <?php echo $nameError;?></span>
<br><br>

E-mail: <input type="text" name="email">


<span class="errorColor">* <?php echo $emailError;?></span>
<br><br>

Message: <textarea name="message" rows="6" cols="24"></textarea>


<br><br>

<input type="submit" name="submit" value="Submit">


</form>

<?php
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $message;
?>

</body>
</html>

The $_SERVER["PHP_SELF"] is a super global variable that returns the filename of the currently
executing script. So, the $_SERVER["PHP_SELF"] sends the submitted form data to the page itself,
instead of jumping to a different page. This way, the user will get error messages on the same page as
the form

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy