0% found this document useful (0 votes)
10 views

PHP Get and Post

Uploaded by

Akshay Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

PHP Get and Post

Uploaded by

Akshay Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

PHP GET AND POST

Get and Post Methods in PHP


• PHP provides two methods through which a client
(browser) can send information to the server. These
methods are given below, and discussed in detail:

• GET method
• POST method
Get and Post Methods in PHP(contd.)
• Get and Post methods are the HTTP request methods
used inside the <form> tag to send form data to the
server.

• HTTP protocol enables the communication between the


client and the server where a browser can be the client,
and an application running on a computer system that
hosts your website can be the server.
GET
• The GET method is used to submit the HTML form data.
This data is collected by the predefined $_GET
variable for processing.

• The information sent from an HTML form using the GET


method is visible to everyone in the browser's address
bar, which means that all the variable names and their
values will be displayed in the URL. Therefore, the get
method is not secured to send sensitive information.
POST
• The POST method is also used to submit the HTML form
data. But the data submitted by this method is collected
by the predefined superglobal variable $_POST instead of
$_GET.

• Unlike the GET method, it does not have a limit on the


amount of information to be sent. The information sent
from an HTML form using the POST method is not visible
to anyone.
$_REQUEST variable
• The $_REQUEST variable is a superglobal variable,
which can hold the content of both $_GET and $_POST
variable.

• In other words, the PHP $_REQUEST variable is used to


collect the form data sent by either GET or POST
methods.
Example 1
Create a file get.php
<html>
<body>

<form action = "gettest.php" method = "GET">


Username: <input type = "text" name = "username" /> <br>
Blood Group: <input type = "text" name = "bloodgroup" />
<br>
<input type = "submit" />
</form>

</body>
</html>
Create a file gettest.php
<html>
<body>

Welcome <?php echo $_GET["username"]; ?> </br>


Your blood group is: <?php echo
$_GET["bloodgroup"]; ?>

</body>
</html>
Execute get.php

OUTPUT:
Welcome Neha
Your blood group is: A+
Example 2
Create a file post.php
<html>
<body>

<form action = "posttest.php" method = "post">


Username: <input type = "text" name = "username" /> <br>
Blood Group: <input type = "text" name = "bloodgroup" />
<br>
<input type = "submit" />
</form>

</body>
</html>
Create a file posttest.php
<html>
<body>

Welcome <?php echo $_POST["username"]; ?> </br>


Your blood group is: <?php echo
$_POST["bloodgroup"]; ?>

</body>
</html>
Execute post.php

OUTPUT:
Welcome Neha
Your blood group is: A+
Example 3
Create a file get-method.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of PHP GET method</title>
</head>
<body>
<?php
if(isset($_GET["name"])){
echo "<p>Hi, " . $_GET["name"] . "</p>";
}
?>
<form method="get" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name:</label>
<input type="text" name="name" id="inputName">
<input type="submit" value="Submit">
</form>
</body>
Execute get-method.php

OUTPUT:
Hi, Neha
Example 4
Create a file post-method.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of PHP POST method</title>
</head>
<body>
<?php
if(isset($_POST["name"])){
echo "<p>Hi, " . $_POST["name"] . "</p>";
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name:</label>
<input type="text" name="name" id="inputName">
<input type="submit" value="Submit">
</form>
</body>
Execute post-method.php

OUTPUT:
Hi, navneet kaur
• Exercise : Calculator using GET Method
• Create a simple calculator using the GET method that
performs addition, subtraction, multiplication, or division
based on user input.
• Requirements:
• Create a form with two input fields for numbers and a dropdown
to select the operation.
• Use the GET method to send the data.
• Display the result based on the selected operation.
• Exercise: Student Grade Calculator
• Task:
• Create a form where a student can input their marks for three
subjects. Based on the total marks, calculate and display the student's
grade (A, B, C, D, or F) according to the following criteria:
• A: 90% and above
• B: 80% – 89%
• C: 70% – 79%
• D: 60% – 69%
• F: Below 60%
• Requirements:
1.Create a form that allows input for three subject marks.
2.Use PHP to calculate the total percentage.
3.Display the grade based on the total percentage.
Exercise 7: Multistep Form
Create a two-step form where the first page collects the user’s
name and email, and the second page asks for their phone number and
address.
Use $_REQUEST to pass the data between the steps.
•Requirements:
1.On the first page, create a form with name and email fields.
2.On the second page, create a form that collects the phone number
and address.
3.Use $_REQUEST to display all the collected data on the second
page.

You might also like