By GokiSoft.com| 09:07 19/04/2021|
Học PHP

[Share Code] Tìm hiểu GET/POST - REQUEST form trong PHP - Xử lý form trong PHP - Lập trình PHP/MySQL

#index.php


<?php
include_once('register.php');
// include('register.php');
// include('register.php');
// require_once('register.php');
// require('register.php');
// require('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($request_pwd != $request_confirm_pwd) {
						echo '<h4 style="color: red">Password not match</h4>';
					}
				?>
			</div>
			<div class="panel-body">
				<form method="post">
					<div class="form-group">
					  <label for="usr">Name:</label>
					  <input required="true" type="text" class="form-control" id="usr" name="fullname" value="<?=$request_fullname?>">
					</div>
					<div class="form-group">
					  <label for="email">Email:</label>
					  <input required="true" type="email" class="form-control" id="email" name="email" value="<?=$request_email?>">
					</div>
					<div class="form-group">
					  <label for="birthday">Birthday:</label>
					  <input type="date" class="form-control" id="birthday" name="birthday" value="<?=$request_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="<?=$request_address?>">
					</div>
					<button class="btn btn-success">Register</button>
				</form>
			</div>
		</div>
	</div>
</body>
</html>


#register.php


<?php
// echo 'TEST';
// var_dump($_REQUEST);
// var_dump($_GET);
// var_dump($_POST);
$request_fullname = $request_email = $request_birthday = $request_pwd = $request_confirm_pwd = $request_address = '';

if(!empty($_POST)) {
	if(isset($_POST['fullname'])) {
		$request_fullname = $_POST['fullname'];
	}
	if(isset($_POST['email'])) {
		$request_email = $_POST['email'];
	}
	if(isset($_POST['birthday'])) {
		$request_birthday = $_POST['birthday'];
	}
	if(isset($_POST['password'])) {
		$request_pwd = $_POST['password'];
	}
	if (isset($_POST['confirmation_pwd'])) {
		$request_confirm_pwd = $_POST['confirmation_pwd'];
	}
	if (isset($_POST['address'])) {
		$request_address = $_POST['address'];
	}

	// echo $request_fullname;
}

if(!empty($_GET)) {
	if(isset($_GET['fullname'])) {
		$request_fullname = $_GET['fullname'];
	}
	if(isset($_GET['email'])) {
		$request_email = $_GET['email'];
	}
	if(isset($_GET['birthday'])) {
		$request_birthday = $_GET['birthday'];
	}
	if(isset($_GET['password'])) {
		$request_pwd = $_GET['password'];
	}
	if (isset($_GET['confirmation_pwd'])) {
		$request_confirm_pwd = $_GET['confirmation_pwd'];
	}
	if (isset($_GET['address'])) {
		$request_address = $_GET['address'];
	}

	// echo $request_fullname;
}

if($request_pwd == $request_confirm_pwd && $request_fullname != null) {
	//nhay sang trang welcome.php
	header('Location: welcome.php?fname='.$request_fullname);
	die();
}


#welcome.php


<!DOCTYPE html>
<html>
<head>
	<title>Welcome PHP</title>
	<meta charset="utf-8">
</head>
<body>
	<?php
		$fullname = '';
		if (isset($_GET['fname'])) {
			$fullname = $_GET['fname'];
		}
	?>
	<h1>Welcome <font color="red"><?=$fullname?></font></h1>
</body>
</html>


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)