我試圖驗證表單使用服務器端,但問題是,我無法顯示錯誤消息和輸入。爲了解釋它的細節:PHP表單驗證服務器端,在頁面上顯示錯誤,在其他頁面上顯示輸出
- 有可以發佈的輸入
- 驗證的數據,例如一個形式,。如果網頁上的空白,顯示錯誤,否則進入到目標頁面
表單頁面,這是我驗證和顯示
<?php
?>
<?=$firstErr?>
<form method="POST" action="">
<input type="text" name="firstname" />
<input type="text" name="secondname" >
<input type="submit" name="submit" >
</form>
目標頁面
if(isset($_POST['submit']))
{
$first = $_POST['firstname'];
$second = $_POST['secondname'];
if($first == "" && $second== "")
{
echo "<script language='javascript'>window.location = 'main.php';</script>";
$firstErr .= '<p class="error">Username should be alpha numeric characters only.</p>';
}
else
{
echo $first;
echo $second;
}
}
爲什麼你不希望顯示錯誤住在同一頁上?你現在正在做一個重定向到main.php,這當然不會顯示$ firstErr的錯誤消息,正如你可能期望的那樣。 – heximal
我需要的只是顯示錯誤,如在同一頁面上該字段爲空,但如果沒有錯誤,則會轉到輸出頁面。 – user2901740