By GokiSoft.com|
09:03 08/04/2022|
Học PHP
[Video] Tìm hiểu GET/POST trong PHP qua ví dụ - Khóa học PHP/MySQL - C2108G3
#bt2837.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Register Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<style type="text/css">
.form-group {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<?php
// Sinh ngẫu nhiên 1 số tự nhiên N từ 3 -> 15
$n = rand(3, 15);
// Tạo mảng $bookList = []
$bookList = [];
// Sinh ngẫu nhiên N mảng key => value gồm các key: title, thumbnail, price -> Add từng mảng key -> value vào mảng $bookList
for ($i=0; $i < $n; $i++) {
$item = [
'title' => 'Sach '.$i,
'thumbnail' => 'https://gokisoft.com/uploads/stores/49/2021/10/coding-c-program.jpg',
'price' => 1000 + 200 * $i
];
$bookList[] = $item;
}
// var_dump($bookList);
?>
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Thumbnail</th>
<th>Title</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
$index = 0;
foreach($bookList as $item) {
echo '<tr>
<td>'.(++$index).'</td>
<td><img src="'.$item['thumbnail'].'" style="width: 100px"/></td>
<td>'.$item['title'].'</td>
<td>'.$item['price'].'</td>
</tr>';
}
?>
</tbody>
</table>
</div>
</body>
</html>
#detail1.php
<?php
$fullname = $email = $password = "";
if(!empty($_GET)) {
if(isset($_GET['fullname'])) {
$fullname = $_GET['fullname'];
}
if(isset($_GET['email'])) {
$email = $_GET['email'];
}
if(isset($_GET['pwd'])) {
$password = $_GET['pwd'];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Register Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<style type="text/css">
.form-group {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<table class="table table-bordered">
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?php
echo $fullname;
?>
</td>
<td>
<?php
print($email);
?>
</td>
<td><?=$password?></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
#detail2.php
<?php
$fullname = $email = $password = "";
if(!empty($_GET)) {
if(isset($_GET['fullname'])) {
$fullname = $_GET['fullname'];
}
if(isset($_GET['email'])) {
$email = $_GET['email'];
}
if(isset($_GET['pwd'])) {
$password = $_GET['pwd'];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Register Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<style type="text/css">
.form-group {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<table class="table table-bordered">
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?php
echo $fullname;
?>
</td>
<td>
<?php
print($email);
?>
</td>
<td><?=$password?></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
#index.php
<?php
if(!empty($_GET)) {
$fullname = $email = $password = "";
if(isset($_GET['fullname'])) {
$fullname = $_GET['fullname'];
}
if(isset($_GET['email'])) {
$email = $_GET['email'];
}
if(isset($_GET['pwd'])) {
$password = $_GET['pwd'];
}
echo "$fullname : $email : $password";
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Register Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<style type="text/css">
.form-group {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="card">
<div class="card-header bg-info text-white">
Register Form
</div>
<div class="card-body">
<form method="get">
<div class="form-group">
<label>Full Name: </label>
<input required type="text" name="fullname" class="form-control">
</div>
<div class="form-group">
<label>Email: </label>
<input required type="email" name="email" class="form-control">
</div>
<div class="form-group">
<label>Password: </label>
<input required type="password" name="pwd" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-info text-white" type="submit">Register</button>
<a href="index.php"><button class="btn btn-warning" type="button">Reset</button></a>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
#index1.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Register Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<style type="text/css">
.form-group {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="card">
<div class="card-header bg-info text-white">
Register Form
</div>
<div class="card-body">
<form method="get" action="detail1.php">
<div class="form-group">
<label>Full Name: </label>
<input required type="text" name="fullname" class="form-control">
</div>
<div class="form-group">
<label>Email: </label>
<input required type="email" name="email" class="form-control">
</div>
<div class="form-group">
<label>Password: </label>
<input required type="password" name="pwd" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-info text-white" type="submit">Register</button>
<a href="index.php"><button class="btn btn-warning" type="button">Reset</button></a>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
#index2.php
<?php
if(!empty($_GET)) {
$fullname = $email = $password = "";
if(isset($_GET['fullname'])) {
$fullname = $_GET['fullname'];
}
if(isset($_GET['email'])) {
$email = $_GET['email'];
}
if(isset($_GET['pwd'])) {
$password = $_GET['pwd'];
}
// echo "detail2.php?fullname=$fullname&email=$email&pwd=$password";
header("Location: detail2.php?fullname=$fullname&email=$email&pwd=$password");
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Register Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<style type="text/css">
.form-group {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="card">
<div class="card-header bg-info text-white">
Register Form
</div>
<div class="card-body">
<form method="get">
<div class="form-group">
<label>Full Name: </label>
<input required type="text" name="fullname" class="form-control">
</div>
<div class="form-group">
<label>Email: </label>
<input required type="email" name="email" class="form-control">
</div>
<div class="form-group">
<label>Password: </label>
<input required type="password" name="pwd" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-info text-white" type="submit">Register</button>
<a href="index.php"><button class="btn btn-warning" type="button">Reset</button></a>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
#post.php
<?php
// get -> $_GET
// post -> $_POST
// get & post -> $_REQUEST
if(!empty($_POST)) {
$fullname = $email = $password = "";
if(isset($_POST['fullname'])) {
$fullname = $_POST['fullname'];
}
if(isset($_POST['email'])) {
$email = $_POST['email'];
}
if(isset($_POST['pwd'])) {
$password = $_POST['pwd'];
}
//Build URL -> chuyen sang trang detail1.php -> GET => Hien thi du lieu trong bang
// echo "$fullname : $email : $password";
header("Location: detail2.php?fullname=$fullname&email=$email&pwd=$password");
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Register Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<style type="text/css">
.form-group {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="card">
<div class="card-header bg-info text-white">
Register Form
</div>
<div class="card-body">
<form method="post">
<div class="form-group">
<label>Full Name: </label>
<input required type="text" name="fullname" class="form-control">
</div>
<div class="form-group">
<label>Email: </label>
<input required type="email" name="email" class="form-control">
</div>
<div class="form-group">
<label>Password: </label>
<input required type="password" name="pwd" class="form-control">
</div>
<div class="form-group">
<button class="btn btn-info text-white" type="submit">Register</button>
<button class="btn btn-warning" type="reset">Reset</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
#readme.txt
Mở rộng yêu cầu: Khi người dùng click Register -> Hiển thị dữ liệu sang trang detail.php
- Chuyển trực tiếp dữ liệu sang trang detail.php
- Chuyển gián tiếp -> từ trang index.php -> Nhận dữ liệu GET -> redirect sang trang detail.php
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)