-1

我预计单击“提交”按钮会用两个回显语句覆盖 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>
4

2 回答 2

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>
于 2013-02-28T19:11:48.653 回答
1

页面的 HTML 超出了if elsePHP 的逻辑,这意味着它将始终显示。我个人会做这样的事情:

<?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; ?>
于 2013-02-28T19:17:28.577 回答