我希望点击“提交”按钮会用两个echo语句覆盖HTML页面。但是,不会发生覆盖。该页面已连接到Apache。
<?php
$username = $_POST['username'];
$password = $_POST['password'];
if (!isset($_POST['submit']))
{
// if page is not submitted to itself echo the form
}
else {
echo("Hello, " . $_POST['username']);
echo("Your password is " . $_POST['password'] . "!");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Visual Debate Home</title>
</head>
<body>
<h1>Visual Debate</h1>
<form method="post" action="?">
<div>Username: </div><div><input type="text" name="username" size="20" maxlength="20"></div>
<div>Password: </div><input type="password" name="password" size="15" maxlength="15"></div>
<div><input type="submit" value="submit" name="submit"></div>
</form>
</body>
</html>
答案 0 :(得分:2)
如果我理解正确,你就是在寻找:
<?php
$username = $_POST['username'];
$password = $_POST['password'];
if (!isset($_POST['submit']))
{
// if page is not submitted to itself echo the form
}
else {
echo("Hello, " . $_POST['username']);
echo("Your password is " . $_POST['password'] . "!");
die(); // stops the script execution! note that you can use die("like this") to output the "like this" and stop the script execution there.
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Visual Debate Home</title>
</head>
<body>
<h1>Visual Debate</h1>
<form method="post" action="?">
<div>Username: </div><div><input type="text" name="username" size="20" maxlength="20"></div>
<div>Password: </div><input type="password" name="password" size="15" maxlength="15"></div>
<div><input type="submit" value="submit" name="submit"></div>
</form>
</body>
</html>
答案 1 :(得分:1)
页面的HTML不在PHP的if
else
逻辑之外,这意味着它将始终显示。我会亲自做这样的事情:
<?php
$username = $_POST['username'];
$password = $_POST['password'];
if (isset($_POST['submit'])):
// Form was submitted to itself -- overwrite the form
echo("Hello, " . $_POST['username']);
echo("Your password is " . $_POST['password'] . "!");
else:
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Visual Debate Home</title>
</head>
<body>
<h1>Visual Debate</h1>
<form method="post" action="?">
<div>Username: </div><div><input type="text" name="username" size="20" maxlength="20"></div>
<div>Password: </div><input type="password" name="password" size="15" maxlength="15"></div>
<div><input type="submit" value="submit" name="submit"></div>
</form>
</body>
</html>
<?php endif; ?>