PHP forms consist of HTML elements and PHP scripts that enable users to input information. When a user submits a form, the data is sent to the server using the HTTP GET or POST method for processing. PHP scripts can then interact with the submitted data to perform various operations, such as inserting the information into a database or sending an email.
GET Method
The HTTP GET request method carries the form’s submitted data in the URL. This method displays the information in the URL including sensitive information. The advantage of using GET is users can bookmark the address with the data since the information is included in the URL.
For example
<form action="welcome.php" method="GET">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
After submitting the form, the information will be sent to the server, and will include the form data in the URL similar to the following:
https://www.example.com/welcome.php?name=John&email=john@example.com
The data can then be accessed on the PHP landing page using the predefined variable $_GET:
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
<!--
**Output:**
Welcome John
Your email address is: john@example.com
-->
POST Method
Unlike GET, the HTTP POST method doesn’t include the data in the URL. This is ideal for collecting sensitive information because the data is not publicly visible.
For example
<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>
After submitting the form, the data can be accessed on the PHP landing page using the predefined variable $_POST:
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
Each method has its advantages and disadvantages. Which one to use depends on your requirements and the purpose of your form. To get started with PHP forms, see PHP Form Generator.
Send Comment: