2014-03-02 28 views
0

我期待將查詢的結果發佈到輸入數據的相同HTML頁面上。我相信這可以通過isset()命令來完成,但我並沒有真正理解它的作用,並告訴我的頁面去一個新的php url給我我的數據表。帶有isset的同一頁上的結果的HTML表單

<html> 
<body> 
<form action="results.php" method="post"> 
<table border="0"> 
<td align="center"><head>Orders</head> 
</tr> 
<tr> 
<td>Enter order number:</td> 
<td align="center"><input type="text" name="enter" size="3" maxlength="3"></td> 
</tr> 
<tr> 
<td colspan="2" align="center"><input type="submit" value="Submit"></td> 
</tr> 
</table> 
</form> 
</body> 
</html> 

這是我現在的代碼。

<form action="results.php" method="post"> 

行告訴代碼張貼到一個新的頁面?

if(isset($_POST)) 

去我的html文件或我的results.php文件中的代碼中的某處?

回答

0

如果你想在同一個文件來顯示結果,

  1. ,你必須重新命名當前文件的html的轉換爲.php使用,而不是
  2. <form method="post" action="<?php echo $PHP_SELF;?>"><form action="results.php" method="post">
  3. 現在又要添加if(isset($_POST))在同一個文件中的某處

您可以照常添加html代碼到.php文件,但不能將php代碼添加到.html文件

0

你可以使用:

if(isset($_POST['enter'])) 

如果你還希望在執行輸出「輸入」字段不爲空值,那麼你可以使用:

if(isset($_POST['enter']) && $_POST['enter']!="") 

還留着表單動作值相同的頁面是這樣的:

<form action="" method="post"> 

OR

<form action="<?php print($_SERVER['PHP_SELF']); ?>" method="post"> 
+0

哪裏的isset代碼去? – user2105982

+1

頁面頂端標籤 – Lab

1

如果你的表單在index.php頁面上,而不是第一件事,那就是將action =「results.php」改爲action =「index.php」,這樣你就不會被重定向到result.php頁面。比你可以做這樣的事情:

<html> 
<body> 
<?php 
    if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
     echo "Order number - " . $_POST['enter']; 
    } 
?> 
<form action="index.php" method="post"> 
<table border="0"> 
<td align="center"><head>Orders</head> 
</tr> 
<tr> 
<td>Enter order number:</td> 
<td align="center"><input type="text" name="enter" size="3" maxlength="3"></td> 
</tr> 
<tr> 
<td colspan="2" align="center"><input type="submit" value="Submit"></td> 
</tr> 
</table> 
</form> 
</body> 
</html> 

如果你想同時填寫表單域輸入的數值比你可以這樣做:

<html> 
<body> 
<?php 
    if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
     echo "Order number - " . $_POST['enter']; 
    } 
?> 
<form action="index.php" method="post"> 
<table border="0"> 
<td align="center"><head>Orders</head> 
</tr> 
<tr> 
<td>Enter order number:</td> 
<td align="center"><input type="text" name="enter" size="3" maxlength="3" value="<?php echo isset($_POST['enter'])?$_POST['enter']:'';?>"></td> 
</tr> 
<tr> 
<td colspan="2" align="center"><input type="submit" value="Submit"></td> 
</tr> 
</table> 
</form> 
</body> 
</html> 
相關問題