In this tutorial, we are going to see how to work with PHP form.
PHP Form :
A PHP form is one key strength that propelled the use of PHP for server-side scripting, because it offers dynamic inputs and outputs, thus acting like any other powerful language such as C, C++ or Java.
PHP form utilize the superglobals $_GET and $_POST. These variables enable dynamic interaction between a user of the website and the server.
The PHP form is used as part of HTML code, from which it obtains information and submits back for display.
PHP Simple Html Form :
<!DOCTYPE HTML>
<html>
<body>
<form action="basic.php" method="post">
<table>
<tr><td>Name:</td><td><input type="text" name="name"></td></tr>
<tr><td>Phone Number</td><td><input type="text" name="phone"></td></tr>
<tr><td colspan="2"><input type="submit"></td></tr>
</table>
</form>
</body>
</html>
Here is the form created in HTML. The “form action” refers the data entered into this form to basic.php using the HTTP POST method.
PHP Form Handling :
Here is the code in PHP, which uses $_POST superglobal to obtain information from the form.
basic.php
<?php
echo "Thank you for the information <font color=green>".$_POST["name"]. "</font>. We will get back to you"; ?><br>
We will call you through this phone number for enquiries: <?php echo $_POST["phone"];
?>
Here is the result that is displayed after opening the HTML file.
Output:
The HTTP GET method may also be used to produce similar results, as shown below.
<?php
echo "Thank you for the information <font color=green>".$_GET["name"]. "</font>. We will get back to you"; ?><br>
We will call you through this phone number for enquiries: <?php echo $_POST["phone"];
?>
The above script also produces the same results that have been shown in the previous browser outputs.
Therefore, if you wish to use either, the choice of yours.
An important point to note is that, this form is very basic and is only meant for basic php form example. Form validation is important for live forms to prevent malicious activities such as spamming or hacking.
The only difference is that $_GET is passed onto a script by the URL parameters while $_POST is passed by the HTTP POST method.
What is PHP $_GET :
The information processed by this superglobal is visible in the URL and is limited to 2000 characters. It is possible to bookmark information because of this feature. However, it is not ideal for login details because passwords are sensitive information, among many other data sets that are private.
What is PHP $_POST :
Any information sent using this method is invisible and has no limits. It also has more features that the get method, including multipart binary input which is ideal for file uploads. However, pages processed with this method cannot be bookmarked because the information is not available as a URL.
It is a secure way of sending sensitive information.
Happy Learning 🙂