Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

Practical-7

1.Write PHP code to implement query string (GET Method).


<!DOCTYPE html>
<html>
<head>
<title>Registration Details</title>
</head>
<body>
<h1>Registration form for student.</h1>
<form action="regdetails.php" method="get" >
First name:
<input type="text" name="first_name" required>
<br><br>
Last name:
<input type="text" name="last_name" required>
<br><br>
Address:
<textarea rows="3" cols="40" name="address"
required>
</textarea>
<br><br>
Gender:
<input type="radio" name="gender">male
<input type="radio" name="gender">female
<br><br>
Birthday:
<input type="date" name="bday" required>
<br><br>
Mobile NO:
<input type="number" name="Mno" required>
<br><br>
E-mail:
<input type="email" name="email" required>
<br><br>
Branch:
<select name="dropdown" required>
<option>Compter</option>
<option>EC</option>
<option>Bio</option>
<option>Civil</option>
</select>
<br><br>
Sem:
<input type="radio" name="sem" value="SEM-1">
SEM-1
<input type="radio" name="sem" value="SEM-2">
SEM-2
<input type="radio" name="sem" value="SEM-3">
SEM-3
<input type="radio" name="sem" value="SEM-4">
SEM-4
<input type="radio" name="sem" value="SEM-5">
SEM-5
<input type="radio" name="sem" value="SEM-6">
SEM-6
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Regdetails.php:

<?php

echo "First name :" . $_GET["first_name"] . "<br />";


echo "Last name :" . $_GET["last_name"] . "<br />";
echo "Address :" . $_GET["address"] . "<br />";
echo "Gender :" . $_GET["gender"] . "<br />";
echo "Birthday :" . $_GET["bday"] . "<br />";
echo "Mobile NO :" . $_GET["Mno"] . "<br />";
echo "E-mail :" . $_GET["email"] . "<br />";
echo "Branch :" . $_GET["dropdown"] . "<br />";
echo "Sem :" . $_GET["sem"] . "<br />";

?>

2. Write PHP code to implement POST Method.


<!DOCTYPE html>
<html>
<head>
<title>Registration Details</title>
</head>
<body>
<h1>Registration form for student.</h1>
<form action="regdetails.php" method="post" >
First name:
<input type="text" name="first_name" required>
<br><br>
Last name:
<input type="text" name="last_name" required>
<br><br>
Address:
<textarea rows="3" cols="40" name="address"
required>
</textarea>
<br><br>
Gender:
<input type="radio" name="gender">male
<input type="radio" name="gender">female
<br><br>
Birthday:
<input type="date" name="bday" required>
<br><br>
Mobile NO:
<input type="number" name="Mno" required>
<br><br>
E-mail:
<input type="email" name="email" required>
<br><br>
Branch:
<select name="dropdown" required>
<option>Compter</option>
<option>EC</option>
<option>Bio</option>
<option>Civil</option>
</select>
<br><br>
Sem:
<input type="radio" name="sem" value="SEM-1">
SEM-1
<input type="radio" name="sem" value="SEM-2">
SEM-2
<input type="radio" name="sem" value="SEM-3">
SEM-3
<input type="radio" name="sem" value="SEM-4">
SEM-4
<input type="radio" name="sem" value="SEM-5">
SEM-5
<input type="radio" name="sem" value="SEM-6">
SEM-6
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Regdetails.php:

<?php

echo "First name :" . $_POST["first_name"] . "<br />";


echo "Last name :" . $_POST["last_name"] . "<br />";
echo "Address :" . $_POST["address"] . "<br />";
echo "Gender :" . $_POST["gender"] . "<br />";
echo "Birthday :" . $_POST["bday"] . "<br />";
echo "Mobile NO :" . $_POST["Mno"] . "<br />";
echo "E-mail :" . $_POST["email"] . "<br />";
echo "Branch :" . $_POST["dropdown"] . "<br />";
echo "Sem :" . $_POST["sem"] . "<br />";

?>

3. Write PHP code to create session variable for user. And


display value of that variable with welcome message in next
page.
S1.php:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="s2.php" method="post">
<label>enter name:</label>
<input type="text" name="fname">
<br>
<br>
<label>enter last name</label>
<input type="text" name="lname">
<br>
<input type="submit" name="submit">
</form>
</body>
</html>
S2.php:

<?php
session_start();

$_SESSION["fistname"]=$_POST['fname'];
$_SESSION["lastname"]=$_POST['lname'];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form method="post" action="s3.php">
<label>enter number:</label>
<input type="text" name="number">
<input type="submit" name="submit">
</form>
</body>
</html>

S3.php:

<?php
session_start();

$_SESSION["no"]=$_POST['number'];
echo "Name:" . $_SESSION['fistname']."<br>";
echo "Lastname:" . $_SESSION['lastname']."<br>";
echo "Number:" . $_SESSION['no']."<br>";
?>

4. Write PHP code to implement cookies.


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> pra7 p4</title>
</head>
<body>
<form method="POST">

User Name<input type="text" name="uname"><br>


Password:<input type="password" name="pwd"><br>
<input type="submit" name="submit">
</form>

<?php
if(isset ($_POST["uname"]))
{
setcookie("uname",$_POST["uname"],time()+10);
header("location:7_4.php");
}
?>
</body>
</html>
<?php
if(isset($_COOKIE['uname']))
{
echo"WELCOME".$_COOKIE["uname"];
}
?>

5. Extra Examples for GET and POST Method.


GET METHOD:
Get.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body style="background-color:white; text-align: center; color:
black;">
<div>
<form method="get" action="getform2.php">
<h1>Registration form</h1>
<br><br><br>
<label>Enter your first name:</label>
<input type="text" name="fname">
<br><br><br>
<label>Enter your last name:</label>
<input type="text" name="lname">
<br><br><br>
<label>Enter your gender:</label>
<input type="radio" name="gender"
value="male">male
<input type="radio" name="gender"
value="female">female
<br><br><br>
<input type="submit" name="submit">
</form>

</div>
</body>
</html>

Getform2.php:

<?php
$firstname=$_GET['fname']."<br>";
$lastname=$_GET['lname']."<br>";
$gender=$_GET['gender']."<br>";

echo $firstname;
echo $lastname;
echo $gender;
?>
POST METHOD:
Post.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body style="background-color: white; text-align: center; color:
black;">
<div>
<form method="post" action="postform2.php">
<h1>Registration form</h1>
<br><br><br>
<label>Enter your first name:</label>
<input type="text" name="fname">
<br><br><br>
<label>Enter your last name:</label>
<input type="text" name="lname">
<br><br><br>
<label>Enter your gender:</label>
<input type="radio" name="gender"
value="male">male
<input type="radio" name="gender"
value="female">female
<br><br><br>
<input type="submit" name="submit">
</form>

</div>
</body>
</html>

Post.php:
<?php
$firstname=$_POST['fname']."<br>";
$lastname=$_POST['lname']."<br>";
$gender=$_POST['gender']."<br>";

echo $firstname;
echo $lastname;
echo $gender;

?>

6. Extra Examples for session and cookies.


SESSION:
Se1.php:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="se2.php" method="post">
<label>enter name:</label>
<input type="text" name="fname">
<br>
<br>
<label>enter last name</label>
<input type="text" name="lname">
<br>
<input type="submit" name="submit">
</form>
</body>
</html>
Se2.php:

<?php
session_start();

$_SESSION["fistname"]=$_POST['fname'];
$_SESSION["lastname"]=$_POST['lname'];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form method="post" action="se3.php">
<label>enter number:</label>
<input type="text" name="number">
<input type="submit" name="submit">
</form>
</body>
</html>
Se3.php:

<?php
session_start();

$_SESSION["no"]=$_POST['number'];

echo $_SESSION['fistname']."<br>";
echo $_SESSION['lastname']."<br>";
echo $_SESSION['no']."<br>";
?>

COOKIE:
Cookie.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> pra7 p4</title>
</head>
<body>
<form method="POST">

User Name<input type="text" name="uname"><br>


Password:<input type="password" name="pwd"><br>
<input type="submit" name="submit">
</form>

<?php
if(isset ($_POST["uname"]))
{
setcookie("uname",$_POST["uname"],time()+10);
header("location:cookie.php");
}
?>
</body>
</html>
<?php
if(isset($_COOKIE['uname']))
{
echo"WELCOME".$_COOKIE["uname"];
}
?>

You might also like