By GokiSoft.com| 09:05 20/04/2022|
Học PHP

[Source Code] Quản lý sinh viên bằng Session - Khóa học PHP/MySQL - C2108G3

Quản lý sinh viên bằng Session - Khóa học PHP/MySQL


#delete_student.php


<?php
session_start();

if(isset($_GET['id'])) {
	$index = $_GET['id'];
	array_splice($_SESSION['studentList'], $index, 1);

	header('Location: register.php');
}


#process_form_register.php


<?php
// Xu ly -> doc duoc du lieu gui tu client len server
$fullname = $email = $pwd = "";

if(!isset($_SESSION['studentList'])) {
	$_SESSION['studentList'] = [];
}

if(!empty($_POST)) {
	if(isset($_POST['fullname'])) {
		$fullname = $_POST['fullname'];
	}
	if(isset($_POST['email'])) {
		$email = $_POST['email'];
	}
	if(isset($_POST['pwd'])) {
		$pwd = $_POST['pwd'];
	}

	$std = [
		'fullname' => $fullname,
		'email' => $email,
		'pwd' => $pwd
	];
	$_SESSION['studentList'][] = $std;
}


#register.php


<?php
session_start();
// require <-> include
require_once('process_form_register.php');
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Quan Ly Sinh Vien</title>
	<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;
		}

		.card {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<div class="card">
		<div class="card-header bg-info text-white">
			Danh Sach Sinh Vien
		</div>
		<div class="card-body">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>No</th>
						<th>Full Name</th>
						<th>Email</th>
						<th>Password</th>
						<th style="width: 50px"></th>
					</tr>
				</thead>
				<tbody>
<?php
$index = 0;
foreach ($_SESSION['studentList'] as $item) {
	echo '<tr>
				<td>'.($index + 1).'</td>
				<td>'.$item['fullname'].'</td>
				<td>'.$item['email'].'</td>
				<td>'.$item['pwd'].'</td>
				<td>
					<a href="delete_student.php?id='.$index.'"><button class="btn btn-danger">Delete</button></a>
				</td>
			</tr>';
	$index++;
}
?>
				</tbody>
			</table>
		</div>
	</div>

	<div class="card">
		<div class="card-header bg-info text-white">
			Nhap Thong Tin Sinh Vien
		</div>
		<div class="card-body">
			<form method="post">
				<div class="form-group">
					<label>Full Name: </label>
					<input type="text" name="fullname" class="form-control">
				</div>
				<div class="form-group">
					<label>Email: </label>
					<input type="email" name="email" class="form-control">
				</div>
				<div class="form-group">
					<label>Password: </label>
					<input type="password" name="pwd" class="form-control">
				</div>
				<div class="form-group">
					<button class="btn btn-info">Register</button>
				</div>
			</form>
		</div>
	</div>
</div>
</body>
</html>


#delete_student2.php


<?php
session_start();

if(isset($_POST['id'])) {
	$index = $_POST['id'];
	array_splice($_SESSION['studentList'], $index, 1);

	header('Location: register2.php');
}


#register2.php


<?php
session_start();
// require <-> include
require_once('process_form_register.php');
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Quan Ly Sinh Vien</title>
	<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;
		}

		.card {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<div class="card">
		<div class="card-header bg-info text-white">
			Danh Sach Sinh Vien
		</div>
		<div class="card-body">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>No</th>
						<th>Full Name</th>
						<th>Email</th>
						<th>Password</th>
						<th style="width: 50px"></th>
					</tr>
				</thead>
				<tbody>
<?php
$index = 0;
foreach ($_SESSION['studentList'] as $item) {
	echo '<tr>
				<td>'.($index + 1).'</td>
				<td>'.$item['fullname'].'</td>
				<td>'.$item['email'].'</td>
				<td>'.$item['pwd'].'</td>
				<td>
					<form method="post" action="delete_student2.php">
						<input type="text" name="id" value="'.$index.'" style="display: none;"/>
						<button class="btn btn-danger">Delete</button>
					</form>
				</td>
			</tr>';
	$index++;
}
?>
				</tbody>
			</table>
		</div>
	</div>

	<div class="card">
		<div class="card-header bg-info text-white">
			Nhap Thong Tin Sinh Vien
		</div>
		<div class="card-body">
			<form method="post">
				<div class="form-group">
					<label>Full Name: </label>
					<input type="text" name="fullname" class="form-control">
				</div>
				<div class="form-group">
					<label>Email: </label>
					<input type="email" name="email" class="form-control">
				</div>
				<div class="form-group">
					<label>Password: </label>
					<input type="password" name="pwd" class="form-control">
				</div>
				<div class="form-group">
					<button class="btn btn-info">Register</button>
				</div>
			</form>
		</div>
	</div>
</div>
</body>
</html>


#register3.php


<?php
session_start();
// require <-> include
require_once('process_form_register.php');
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Quan Ly Sinh Vien</title>
	<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>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.form-group {
			margin-bottom: 20px;
		}

		.card {
			margin-bottom: 20px;
		}
	</style>
</head>
<body>
<div class="container">
	<div class="card">
		<div class="card-header bg-info text-white">
			Danh Sach Sinh Vien
		</div>
		<div class="card-body">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>No</th>
						<th>Full Name</th>
						<th>Email</th>
						<th>Password</th>
						<th style="width: 50px"></th>
					</tr>
				</thead>
				<tbody>
<?php
$index = 0;
foreach ($_SESSION['studentList'] as $item) {
	echo '<tr>
				<td>'.($index + 1).'</td>
				<td>'.$item['fullname'].'</td>
				<td>'.$item['email'].'</td>
				<td>'.$item['pwd'].'</td>
				<td>
					<button class="btn btn-danger" onclick="deleteStudent('.$index.')">Delete</button>
				</td>
			</tr>';
	$index++;
}
?>
				</tbody>
			</table>
		</div>
	</div>

	<div class="card">
		<div class="card-header bg-info text-white">
			Nhap Thong Tin Sinh Vien
		</div>
		<div class="card-body">
			<form method="post">
				<div class="form-group">
					<label>Full Name: </label>
					<input type="text" name="fullname" class="form-control">
				</div>
				<div class="form-group">
					<label>Email: </label>
					<input type="email" name="email" class="form-control">
				</div>
				<div class="form-group">
					<label>Password: </label>
					<input type="password" name="pwd" class="form-control">
				</div>
				<div class="form-group">
					<button class="btn btn-info">Register</button>
				</div>
			</form>
		</div>
	</div>
</div>

<script type="text/javascript">
	function deleteStudent(index) {
		var option = confirm('Ban chac chan muon xoa sinh vien nay ko?')
		if(!option) return

		$.post('delete_student2.php', {
			'id': index
		}, function(data) {
			console.log('delete success')
			location.reload()
		})
	}
</script>
</body>
</html>


Tags:



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

5

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

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó