We can create and use forms in PHP. To get from data, we need to use PHP superglobals $_GET and $_POST.
The form request maybe gets or post. To retrieve data from getting requests, we need to use $_GET, for post request $_POST.
Get request is the default form request. The data passed through getting requests is visible on the URL browser so it is not secured. You can send a limited amount of data through getting requests.
Let's see a simple example to receive data from get requests in PHP.
File Name: fisrt_form.html
<form action="welcome.php" method="get">
Name: <input type="text" name="name"/>
<input type="submit" value="visit"/>
</form>
File Name: welcome.php
<?php
$name=$_GET["name"];//receiving name field value in $name variable
echo "Welcome, $name";
?>
Post request is widely used to submit the form that has a large amount of data such as file upload, image upload, login form, registration form, etc.
The data passed through post request is not visible on the URL browser so it is secured. You can send a large amount of data through post requests.
Let's see a simple example to receive data from post requests in PHP.
File Name: firstform.html
<form action="login.php" method="post">
<table>
<tr><td>Name:</td><td> <input type="text" name="name"/></td></tr>
<tr><td>Password:</td><td> <input type="password" name="password"/></td></tr>
<tr><td colspan="2"><input type="submit" value="login"/> </td></tr>
</table>
</form>
File: login.php
<?php
$name=$_POST["name"];//receiving name field value in $name variable
$password=$_POST["password"];//receiving password field value in $password variable
echo "Welcome: $name, your password is: $password";
?>
Output:
Name :
Password :
Submit Button
After Click On Submit Button Then Authentication Success
Output:
Welcome: User_name, your password is: user_password
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning