PHP Get and Post
PHP Get and Post
• 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.
</body>
</html>
Create a file gettest.php
<html>
<body>
</body>
</html>
Execute get.php
OUTPUT:
Welcome Neha
Your blood group is: A+
Example 2
Create a file post.php
<html>
<body>
</body>
</html>
Create a file posttest.php
<html>
<body>
</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.