我有两个页面:
一个是index.php,里面有load_rc按钮导航,在这个页面中还有一个
以下是两个页面的代码:
首先是index.php的:
<?php
// 启用错误报告
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once './connections/games.php';
// 显示页面内容
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>2024</title>
<link href="./css/styles.css" rel="stylesheet" type="text/css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
// 加载 rc.php 到 <main> 标签
$('#load_rc').click(function(e) {
e.preventDefault();
$('#main-content').load('rc.php', function() {
// rc.php 内容加载完成后的回调函数
});
});
// 处理rc.php中的表单提交
$('#rc-form').submit(function(e) {
e.preventDefault();
var formData = $(this).serialize();
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: formData,
success: function(response) {
// 将查询结果显示在<main>标签中
$('#main-content').html(response);
},
error: function() {
alert('查询失败!');
}
});
});
});
function loadDjcx() {
$.ajax({
url: 'rc.php',
success: function(response) {
// 将rc.php的内容加载到<main>标签中
$('#main-content').html(response);
},
error: function() {
alert('加载失败!');
}
});
}
</script>
</head>
<body>
<div class="app">
<header class="header">
<a class="link-logo"><img src="./image/bdh_bg.jpg" width=90% height=100%></a>
</header>
<div class="container">
<div class="nav">
<button class="button" type="button" id="load_rc">日程 | Schedule</button>
</div>
<div class="main">
<main id="main-content"></main>
</div>
</div>
</body>
</html>
然后是rc.php的:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once './connections/games.php';
?>
<?php
$stmt = $pdo->prepare('SELECT DISTINCT `date` FROM bdh_date');
$stmt->execute();
$dateList = $stmt->fetchAll(PDO::FETCH_COLUMN);
$selectedDate = isset($_GET['date']) ? $_GET['date'] : '';
$selectedApm = isset($_GET['apm']) ? $_GET['apm'] : '';
$selectedPlace = isset($_GET['place']) ? $_GET['place'] : '';
$selectedOrder = isset($_GET['order']) ? $_GET['order'] : '';
foreach ($dateList as $date) {}
if (!empty($selectedDate)) {
$apmStmt = $pdo->prepare('SELECT DISTINCT `apm` FROM bdh_date WHERE `date` = :date');
$apmStmt->bindParam(':date', $selectedDate);
$apmStmt->execute();
$apmList = $apmStmt->fetchAll(PDO::FETCH_COLUMN);
if (!empty($selectedApm)) {
$placeStmt = $pdo->prepare('SELECT DISTINCT `place` FROM bdh_date WHERE `date` = :date AND `apm` = :apm');
$placeStmt->bindParam(':date', $selectedDate);
$placeStmt->bindParam(':apm', $selectedApm);
$placeStmt->execute();
$placeList = $placeStmt->fetchAll(PDO::FETCH_COLUMN);
if (!empty($selectedPlace)) {
$orderStmt = $pdo->prepare('SELECT DISTINCT `order` FROM bdh_date WHERE `date` = :date AND `apm` = :apm AND `place` = :place');
$orderStmt->bindParam(':date', $selectedDate);
$orderStmt->bindParam(':apm', $selectedApm);
$orderStmt->bindParam(':place', $selectedPlace);
$orderStmt->execute();
$orderList = $orderStmt->fetchAll(PDO::FETCH_COLUMN);
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>竞赛日程 | 2024“滑启杯”第十六届北戴河自由式轮滑公开赛</title>
</head>
<body>
<div class=""><a>竞赛日程 | Schedule</a></div>
<div class="" style="border-radius: 15px; background-color: #9cc65f";>
<form id="rc-form" action="" method="get">
<!-- 第1个下拉框 -->
<select id="date-select" name="date" onchange="this.form.submit();">
<option value="">请选择</option>
<?php foreach ($dateList as $date): ?>
<option value="<?php echo htmlspecialchars($date); ?>" <?php echo $date === $selectedDate ? 'selected' : ''; ?>><?php echo htmlspecialchars($date); ?></option>
<?php endforeach; ?>
</select>
<!-- 第2个下拉框 -->
<select id="apm-select" name="apm" onchange="this.form.submit();">
<option value="">请选择</option>
<?php foreach ($apmList as $apm): ?>
<option value="<?php echo htmlspecialchars($apm); ?>" <?php echo $apm === $selectedApm ? 'selected' : ''; ?>><?php echo htmlspecialchars($apm); ?></option>
<?php endforeach; ?>
</select>
<!-- 第3个下拉框 -->
<select id="place-select" name="place" onchange="this.form.submit();">
<option value="">请选择</option>
<?php foreach ($placeList as $place): ?>
<option value="<?php echo htmlspecialchars($place); ?>" <?php echo $place === $selectedPlace ? 'selected' : ''; ?>><?php echo htmlspecialchars($place); ?></option>
<?php endforeach; ?>
</select>
</form>
</div>
<script>
function handleSelectChange(event) {
// 阻止表单的默认提交行为
event.preventDefault();
// 获取当前选中的值
var date = document.getElementById('date-select').value;
var apm = document.getElementById('apm-select').value;
var place = document.getElementById('place-select').value;
// 使用AJAX发送请求到服务器
$.ajax({
url: 'rc.php', // 或者你的服务器端处理逻辑所在的URL
type: 'POST', // 或者'POST',取决于你的服务器端处理逻辑
data: {
date: date,
apm: apm,
place: place
},
success: function(response) {
// 假设服务器返回的是表格的HTML内容
$('#main-content').html(response);
},
error: function(xhr, status, error) {
console.error("Error fetching data", error);
}
});
}
</script>
<!--选定之后,显示比赛信息区域 -->
<?php
// 确保输入是安全的
$date = $_GET['date'] ?? '';
$apm = $_GET['apm'] ?? '';
$place = $_GET['place'] ?? '';
$order = $_GET['order'] ?? '';
// 从记录集中调取出对应信息
$stmt = $pdo->prepare("
SELECT
`order`,
now,
time,
`group`
FROM
bdh_date
WHERE
`date` = ? AND
`apm` = ? AND
`place` = ?
ORDER BY
`order` ASC
");
$stmt->execute([$date, $apm, $place]);
$order = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!--选定之后,显示比赛信息区域 -->
<div name="div_main" class="" style="height:20px;border-radius: 20px; ">
</div>
<div name="div_main" class="" style="border-radius: 20px; background-color:#545454;">
<table class="" style="font-family: inherit;">
<tr align="center" style="font-weight:bold;font-size:12px;">
<td>顺序<br>Order</td>
<td>状态<br>State</td>
<td>时间<br>Time</td>
<td>组别<br>Group</td>
</tr>
<?php
foreach ($order as $row):
?>
<tr align="center" style="font-weight:bold;font-size:10px;">
<td><?php echo htmlspecialchars($row['order']);?></td>
<td><?php echo htmlspecialchars($row['now']); ?></td>
<td><?php echo htmlspecialchars($row['time']); ?></td>
<td><?php echo htmlspecialchars($row['group']); ?></td>
<td><?php echo htmlspecialchars($row['event']); ?></td>
</tr>
<?php
endforeach;
?>
</table>
</div>
</div>
</body>
</html>