PHP 表单提交后跳转页面是指用户在网页上填写表单信息并提交后,服务器处理完这些信息后,将用户重定向到另一个页面。这种跳转可以通过多种方式实现,例如使用 header() 函数、JavaScript 或者 HTML 的 <meta> 标签。
header() 函数进行跳转。window.location 进行跳转。<meta> 标签的 http-equiv="refresh" 属性进行跳转。header() 函数)<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 处理表单数据
$username = $_POST['username'];
$password = $_POST['password'];
// 假设处理成功
header('Location: success.php');
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>表单提交</title>
</head>
<body>
<form method="post" action="">
<label for="username">用户名:</label>
<input type="text" id="username" name="username">
<br>
<label for="password">密码:</label>
<input type="password" id="password" name="password">
<br>
<input type="submit" value="提交">
</form>
</body>
</html><?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 处理表单数据
$username = $_POST['username'];
$password = $_POST['password'];
// 假设处理成功
echo '<script>window.location.href = "success.php";</script>';
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>表单提交</title>
</head>
<body>
<form method="post" action="">
<label for="username">用户名:</label>
<input type="text" id="username" name="username">
<br>
<label for="password">密码:</label>
<input type="password" id="password" name="password">
<br>
<input type="submit" value="提交">
</form>
</body>
</html>header() 函数未生效原因:
header() 函数之前已经输出了内容。header() 函数调用后没有 exit() 或 die() 终止脚本执行。解决方法:
header() 函数之前没有任何输出,包括空格、换行等。header() 函数调用后添加 exit() 或 die()。<?php
ob_start(); // 开启输出缓冲
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 处理表单数据
$username = $_POST['username'];
$password = $_POST['password'];
// 假设处理成功
header('Location: success.php');
exit();
}
ob_end_flush(); // 输出缓冲内容并关闭缓冲
?>
<!DOCTYPE html>
<html>
<head>
<title>表单提交</title>
</head>
<body>
<form method="post" action="">
<label for="username">用户名:</label>
<input type="text" id="username" name="username">
<br>
<label for="password">密码:</label>
<input type="password" id="password" name="password">
<br>
<input type="submit" value="提交">
</form>
</body>
</html>原因:
解决方法:
window.onload 或 DOMContentLoaded 事件。<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 处理表单数据
$username = $_POST['username'];
$password = $_POST['password'];
// 假设处理成功
echo '<script>window.onload = function() { window.location.href = "success.php"; };</script>';
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>表单提交</title>
</head>
<body>
<form method="post" action="">
<label for="username">用户名:</label>
<input type="text" id="username" name="username">
<br>
<label for="password">密码:</label>
<input type="password" id="password" name="password">
<br>
<input type="submit" value="提交">
</form>
</body>
</html>没有搜到相关的文章