Practical-05
Write a PHP Program that will use the concept form.
Introduction
This introduction will provide students with a basic understanding of forms and the functionality of forms in PHP. Forms in PHP are interactive HTML elements. They retrieve user input and send it to a server which processes it and returns a response. Forms are used for a wide range of tasks such as submitting a job application, entering contact information, registering for an event and many more scenarios. When creating forms, there are a few important considerations that students should keep in mind such as adding labels, setting the method type, and organizing fields before submitting. This article will provide students with tips and advice to create forms using PHP while following best practices.
<?php
// form elements
$name = "";
$email = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
Name: <input type="text" name="name" value="<?php echo $name;?>">
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<input type="submit" value="Submit">
</form>
Output
Output of the code should be a form that has two input fields, a name and email, and a submit button.
Explanation
The code creates a simple form with two input fields and a submit button. These inputs are initialized with empty strings, and then their values are saved in the form of PHP variables if the form is submitted using the POST request. The action attribute of the form is set to the same page, and the request method is POST.