In this episode, we will look at one of the most important methods of PHP. It is the use of GET and POST methods.
When we submit a form or information to the server, we submit that information using the GET or POS method.
If we use the gate method, all the information in the form can be seen in the URL which is not secure. However, if we send the values using the post method, then no information or data can be seen in the URL. This is why the post method from the gate is a more secure method.
So let's get started First, let's create a PHP file and then create a form with a username and password field. As below,
get_and_post.php
Source Code:
So let's get started First, let's create a PHP file and then create a form with a username and password field. As below,
get_and_post.php
<html>
<head>
<title>PHP get and post method</title>
</head>
<body>
<form action="get_and_post.php" method="post">
<label>Name:</label>
<input type="text" name="name" placeholder="Enter name">
<br>
<br>
<label>Password:</label>
<input type="password" name="password" placeholder="Enter password">
<br>
<br>
<input type="submit" name="login-btn" value="login account">
</form>
</body>
</html>
Of course, I have written a name attribute in each field because we will fetch the values of the field by holding this name attribute. I even took the name attribute for the button.
Now at the top of the file I will write all the code between the PHP opening and closing tags. First we will fetch the name and password using the Just echo function. So I will print using $ _GET [‘name_attribute’].
<?php
//echo $_GET['name']."<br>"; // testing purpose
//echo $_GET['password'];
//if(isset($_GET['login-btn'])){
// echo $_GET['name']."<br>";
// echo $_GET['password'];
//}
if(isset($_POST['login-btn'])){
echo $_POST['name']."<br>";
echo $_POST['password'];
}
?>
If we run the file then we can see that the values given are.
And if you notice, you will see the URL bar and the values.
Source Code:
<?php
//echo $_GET['name']."<br>";
//echo $_GET['password'];
//if(isset($_GET['login-btn'])){
// echo $_GET['name']."<br>";
// echo $_GET['password'];
//}
if(isset($_POST['login-btn'])){
echo $_POST['name']."<br>";
echo $_POST['password'];
}
?>
<html>
<head>
<title>PHP get and post method</title>
</head>
<body>
<form action="get_and_post.php" method="post">
<label>Name:</label>
<input type="text" name="name" placeholder="Enter name">
<br>
<br>
<label>Password:</label>
<input type="password" name="password" placeholder="Enter password">
<br>
<br>
<input type="submit" name="login-btn" value="login account">
</form>
</body>
</html>
And if we post the method of the form equally, then the good ones will not be seen in the URL bar. Here you have to fetch the value using $ _POST [‘name_attribute’].
stay tunned...
Post a Comment
Leave a comment first....