PHP(Hypertext Preprocessor)是一种通用开源脚本语言,主要用于服务器端开发。它能够嵌入HTML代码中,使得网页具有动态内容。当用户提交表单时,PHP可以接收这些数据并处理,然后返回相应的页面。
PHP返回提交页面的方式主要有以下几种:
header()
函数将用户重定向到另一个页面。include()
或require()
函数将另一个PHP文件的内容包含进来。<?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'];
// 假设验证成功
include '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 '<!DOCTYPE html>';
echo '<html>';
echo '<head><title>成功页面</title></head>';
echo '<body>';
echo '<h1>登录成功!欢迎 ' . htmlspecialchars($username) . '。</h1>';
echo '</body>';
echo '</html>';
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如何处理表单提交并返回相应的页面。根据具体需求选择合适的方式进行处理。
领取专属 10元无门槛券
手把手带您无忧上云