Processing the form User Input
Processing HTML
form user input in PHP involves capturing the data submitted through the form
and handling it on the server side.
Follow the
following steps:
1. Create HTML Form:
Create an HTML
form in a file (e.g., `index.html`) with the necessary input fields.
The form's
`action` attribute should point to the PHP script that will process the form
data.
<!DOCTYPE
html>
<html
lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>PHP Form
Processing</title>
</head>
<body>
<form
action="process_form.php" method="post">
<label
for="name">Name:</label>
<input type="text"
id="name" name="name" required>
<label
for="email">Email:</label>
<input type="email"
id="email" name="email" required>
<input type="submit"
value="Submit">
</form>
</body>
</html>
2. Create PHP Script for Form Processing:
Create a PHP
script (e.g., `process_form.php`) to handle the form data.
This script will
receive the form data using the `$_POST` superglobal.
<?php
// Check if the
form is submitted
if ($_SERVER["REQUEST_METHOD"]
== "POST") {
// Retrieve form data
$name =
htmlspecialchars($_POST["name"]);
$email =
htmlspecialchars($_POST["email"]);
// Process the data (you can perform
validation, database operations, etc. here)
// Display the submitted data (for
demonstration purposes)
echo "Name: $name <br>";
echo "Email: $email";
}
else
{
// If the form is not submitted, redirect
to the form page
header("Location: index.html");
exit();
}
?>
0 Comments