Php 使用$\u POST覆盖HTML页面

Php 使用$\u POST覆盖HTML页面,php,post,overwrite,Php,Post,Overwrite,我希望单击“提交”按钮会用两条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, " . $_

我希望单击“提交”按钮会用两条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>

视觉辩论之家
视觉辩论
用户名:
密码:

如果我正确理解了您的意思,那么您正在寻找:

<?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>

视觉辩论之家
视觉辩论
用户名:
密码:

页面的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; ?>

视觉辩论之家
视觉辩论
用户名:
密码:

HTML和表单将无条件地输出到您发布的代码中。但是,在提交表单时,您打印的消息应该显示在HTML代码上方。我最初按照您的方式安排代码,但我遇到了相同的问题。我正在运行您的代码,但仍然没有结果。是的,如果您在中运行我的代码(或Zlatan的),您将看到这两种方法都有效。您的服务器/PHP配置有问题。我也试着运行这段代码。没有结果。我认为这是一个IDE问题。谢谢大家。IDE不应该是一个问题。