2013-02-28 102 views
-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> 
+0

HTML和表單無條件地輸出在您發佈的代碼中。當提交表單時,您打印的信息應顯示在HTML代碼上方。 – drew010 2013-02-28 19:13:00

回答

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> 
+0

我試着運行這個確切的代碼。沒有結果。我認爲這是一個IDE問題。謝謝你們。 – 2013-02-28 21:43:06

+1

IDE應該不成問題。 – 2013-02-28 23:07:57

1

這個頁面的HTML是你的PHP的ifelse邏輯之外,這意味着它會一直顯示。我會親自做這樣的事情:

<?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; ?> 
+0

我最初安排我的代碼,因爲你有,但我有同樣的問題。我正在運行您的確切代碼,但仍然沒有結果。 – 2013-02-28 21:40:32

+1

是的,如果你在[PhpFiddle](http://phpfiddle.org/)中運行我的代碼(或Zlatan的),你會發現這兩種方法都行得通。有些東西與你的服務器/ PHP配置有關。 – Liv 2013-03-01 12:20:42