Let’s create a basic form that takes a user's name and email address and then processes and displays the input using PHP.
Creating two files, one for the HTML form in html format (‘form.html’) and second one for the PHP in php format
(‘formData.php’).
<h2>PHP Form</h2>
<form action="formData.php" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br><br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Collect form data
$name = $_POST["name"];
$email = $_POST["email"];
// Display the form data
echo "<p>Name: $name</p>";
echo "<p>Email: $email</p>";
}
?>
When the user fills the form and clicks the submit button, the form data is sent to the “formData.php” with the HTTP POST method.
The work flow:
Note: HMTL form submitting can be achieved using GET method. $_GET and $_POST are php superglobals which are used to collect data from the Form.
Get and Post are the two methods that the browser client can send information to the server. These methods are commonly used with HTML forms.