By GokiSoft.com| 15:22 21/09/2020|
Học PHP

[Share Code] Tìm hiểu Form (GET/POST) lập trình PHP - Lập trình PHP MySQL

#process_form_user.php


<?php
var_dump($_GET);
var_dump($_POST);
var_dump($_REQUEST);
$g_name = $g_email = $g_password = $g_confirmation_pwd = $g_address = $g_birthday = '';

if (!empty($_GET)) {
	if (isset($_GET['name'])) {
		$g_name = $_GET['name'];
	}
	if (isset($_GET['email'])) {
		$g_email = $_GET['email'];
	}
	if (isset($_GET['password'])) {
		$g_password = $_GET['password'];
	}
	if (isset($_GET['confirmation_pwd'])) {
		$g_confirmation_pwd = $_GET['confirmation_pwd'];
	}
	if (isset($_GET['address'])) {
		$g_address = $_GET['address'];
	}
	if (isset($_GET['birthday'])) {
		$g_birthday = $_GET['birthday'];
	}
}

if (!empty($_POST)) {
	if (isset($_POST['name'])) {
		$g_name = $_POST['name'];
	}
	if (isset($_POST['email'])) {
		$g_email = $_POST['email'];
	}
	if (isset($_POST['password'])) {
		$g_password = $_POST['password'];
	}
	if (isset($_POST['confirmation_pwd'])) {
		$g_confirmation_pwd = $_POST['confirmation_pwd'];
	}
	if (isset($_POST['address'])) {
		$g_address = $_POST['address'];
	}
	if (isset($_POST['birthday'])) {
		$g_birthday = $_POST['birthday'];
	}
}

// echo $g_name.'-'.$g_email;
//name => rong, mat khau khong khop hoac rong => quay sang trang vidu.php
//du lieu dung => welcome.php

if ($g_name == '' || $g_password == '' || $g_confirmation_pwd == '' || $g_password != $g_confirmation_pwd) {
	//quay sang trang vidu.php
	header('Location: vidu.php');
} else {
	//trang welcome
	header('Location: welcome.php?name='.$g_name);
}


#vidu.php


<?php
// require_once ('process_form_user.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>
			</div>
			<div class="panel-body">
				<!-- method: get/post -->
				<!-- Tim hieu GET: du lieu se duoc gui tren URL -->
				<!-- Tim hieu POST: du lieu se duoc gui ngam -->
				<form method="post" action="process_form_user.php">
					<div class="form-group">
					  <label for="usr">Name:</label>
					  <input required="true" type="text" class="form-control" id="usr" name="name">
					</div>
					<div class="form-group">
					  <label for="email">Email:</label>
					  <input required="true" type="email" class="form-control" id="email" name="email">
					</div>
					<div class="form-group">
					  <label for="birthday">Birthday:</label>
					  <input type="date" class="form-control" id="birthday" name="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">
					</div>
					<button class="btn btn-success">Register</button>
				</form>
			</div>
		</div>
	</div>
</body>
</html>


#welcome.php


<!DOCTYPE html>
<html>
<head>
	<title>Welcome Page</title>
</head>
<body>
<?php
$g_name = '';
if (isset($_GET['name'])) {
	$g_name = $_GET['name'];
}
echo date('d/m/Y H:i:s');

function plus($x, $y) {
	$s = $x+$y;
	echo '<br/>'.$s;
}

plus(1, 6);

//overloading => viet ten ham => khac nhau ve tham so
//PHP => ko co tinh nang overloading.
function plus2($x = 0, $y = 0) {
	return $x+$y;
}
$s = plus2();
echo '<br/>'.$s;
?>
	<h1>Welcome <font color="red"><?=$g_name?></font></h1>
</body>
</html>


Tags:

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

5

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