By GokiSoft.com|
19:37 04/05/2021|
Học PHP
[Share Code] Tìm hiểu GET/POST - REQUEST trong lập trình PHP/MySQL
#welcome.php
<!DOCTYPE html>
<html>
<head>
<title>Welcome Page</title>
<meta charset="utf-8">
</head>
<body>
<?php
$uname = '';
if (isset($_GET['uname'])) {
$uname = $_GET['uname'];
}
?>
<h1>Welcome <font color="red"><?=$uname?></font></h1>
</body>
</html>
#register.php
<?php
require_once ('form_process.php');
// require_once ('form_register.php');
// require ('form_register.php');
// include_once ('form_register.php');
// include ('form_register.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Registation Form * Form Tutorial</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center">Registation Form - Input User's Detail Information</h2>
<?php
if (!empty($_POST)) {
if (!empty($username) && $password == $confirmation_pwd && !empty($password)) {
} else {
echo '<h2 style="color: red;">Register failed (check username & password again)</h2>';
}
}
?>
</div>
<div class="panel-body">
<!-- method: get/post -->
<form method="post">
<div class="form-group">
<label for="usr">Name:</label>
<input required="true" type="text" class="form-control" id="usr" name="username" value="<?php echo $username;?>">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input required="true" type="email" class="form-control" id="email" name="email" value="<?=$email?>">
</div>
<div class="form-group">
<label for="birthday">Birthday:</label>
<input type="date" class="form-control" id="birthday" name="birthday" value="<?=$birthday?>">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input required="true" type="password" class="form-control" id="pwd" name="password">
</div>
<div class="form-group">
<label for="confirmation_pwd">Confirmation Password:</label>
<input required="true" type="password" class="form-control" id="confirmation_pwd" name="confirmation_pwd">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" name="address" value="<?=$address?>">
</div>
<button type="submit" class="btn btn-success">Register</button>
<button type="reset" class="btn btn-warning">Reset</button>
</form>
</div>
</div>
</div>
</body>
</html>
#form_register.php
<?php
echo '<br/>ok<br/>';
// var_dump($_POST);
// var_dump($_GET);
// var_dump($_REQUEST);
// $get_username = $get_email = $get_birthday = $get_password = $get_confirmation_pwd = $get_address = "";
// if (!empty($_GET)) {
// echo 'GET method<br/>';
// if (isset($_GET['username'])) {
// $get_username = $_GET['username'];
// }
// if (isset($_GET['email'])) {
// $get_email = $_GET['email'];
// }
// if (isset($_GET['birthday'])) {
// $get_birthday = $_GET['birthday'];
// }
// if (isset($_GET['password'])) {
// $get_password = $_GET['password'];
// }
// if (isset($_GET['confirmation_pwd'])) {
// $get_confirmation_pwd = $_GET['confirmation_pwd'];
// }
// if (isset($_GET['address'])) {
// $get_address = $_GET['address'];
// }
// echo $get_username.'-'.$get_address;
// }
$post_username = $post_email = $post_birthday = $post_password = $post_confirmation_pwd = $post_address = "";
if (!empty($_POST)) {
echo 'POST method<br/>';
//$_POST: array trong PHP: key -> value
if (isset($_POST['username'])) {
$post_username = $_POST['username'];
}
if (isset($_POST['email'])) {
$post_email = $_POST['email'];
}
if (isset($_POST['birthday'])) {
$post_birthday = $_POST['birthday'];
}
if (isset($_POST['password'])) {
$post_password = $_POST['password'];
}
if (isset($_POST['confirmation_pwd'])) {
$post_confirmation_pwd = $_POST['confirmation_pwd'];
}
if (isset($_POST['address'])) {
$post_address = $_POST['address'];
}
echo $post_username.'-'.$post_address;
}
// $request_username = $request_email = $request_birthday = $request_password = $request_confirmation_pwd = $request_address = "";
// if (!empty($_REQUEST)) {
// echo 'REQUEST method<br/>';
// if (isset($_REQUEST['username'])) {
// $request_username = $_REQUEST['username'];
// }
// if (isset($_REQUEST['email'])) {
// $request_email = $_REQUEST['email'];
// }
// if (isset($_REQUEST['birthday'])) {
// $request_birthday = $_REQUEST['birthday'];
// }
// if (isset($_REQUEST['password'])) {
// $request_password = $_REQUEST['password'];
// }
// if (isset($_REQUEST['confirmation_pwd'])) {
// $request_confirmation_pwd = $_REQUEST['confirmation_pwd'];
// }
// if (isset($_REQUEST['address'])) {
// $request_address = $_REQUEST['address'];
// }
// echo $request_username.'-'.$request_address;
// }
// Fix error cho code phan 2:
$username = $email = $birthday = $password = $confirmation_pwd = $address = "";
#form_process.php
<?php
$username = $email = $birthday = $password = $confirmation_pwd = $address = "";
if (!empty($_POST)) {
if (isset($_POST['username'])) {
$username = $_POST['username'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
if (isset($_POST['birthday'])) {
$birthday = $_POST['birthday'];
}
if (isset($_POST['password'])) {
$password = $_POST['password'];
}
if (isset($_POST['confirmation_pwd'])) {
$confirmation_pwd = $_POST['confirmation_pwd'];
}
if (isset($_POST['address'])) {
$address = $_POST['address'];
}
//Kiem tra dieu kien dang ky thanh cong
if (!empty($username) && $password == $confirmation_pwd && !empty($password)) {
//Register thanh cong
// echo 'dky thanh cong';
header('Location: welcome.php?uname='.$username);
die();
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)