PHP Form Handling
PHP Form Handling
PHP Form Handling
</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>
</body>
</html>
Welcome ABC
Your email address is ABC@example.com
………………………………………………………………………………………………………………
…………………………………………..
The same result could also be achieved using the HTTP GET method:
<html>
<body>
</body>
</html>
<html>
<body>
</body>
</html>
…………………………………
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.
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
However, because the variables are not displayed in the URL, it is not possible
to bookmark the page.
{
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;
}
?>
<?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