2013-10-05 26 views
0

如果用戶未提交姓名,性別或宿舍,按「註冊」按鈕後該頁面應該顯示紅色錯誤。這是我的代碼,但它運行不正常。它始終將錯誤給用戶。檢查表單是否填寫並給出信息

<?php 
//checking if submitted 

if(isset($_POST["action"])) 
{ 
    if (empty($_POST["name"]) || empty($_POST["gender"]) || empty($_POST["dorm"])) 
     $error=true; 
} 
?> 

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <title>Fresh IMs</title> 
    </head> 
    <body> 
     <div style="text-align:center"> 
      <h1>Register for Fresh IMs</h1> 
      <br/><br/> 
      <? if(isset($error)): ?> 
       <div style="color:red"> You must fill out the form!</div> 
      <? endif ?> 
      <form action="index.php" method="post"> 
       <table style="border:0; margin-left:auto; margin-right:auto"> 
        <tr> 
         <td>Name:</td> 
         <td><input name="name" type="text"></td> 
        </tr> 
        <tr> 
         <td>Captain:</td> 
         <td><input name="captain" type="checkbox"></td> 
        </tr> 
        <tr> 
         <td>Gender:</td> 
         <td> 
          <input name="gender" type="radio" value="F">F 
          <input name="gender" type="radio" value="M">M 
         </td>      
        </tr> 
        <tr> 
         <td>Dorm:</td> 
         <td> 
          <select name="dorm"> 
           <option value="" ></option> 
           <option value="Apply court" >Apply Court</option> 
           <option value="Candy">Candy</option> 
          </select> 
         </td> 
        </tr> 
       </table> 
       <input name="action" type="submit" value="Register"> 
      </form> 
     </div> 
    </body> 
</html> 
+0

你有什麼錯誤? – DS9

+0

這是寫在index.php上的代碼嗎? – w3b

+0

yes index.php .. – pacmanfordinner

回答

0

問題是應該是一個你錯過了php tag

<?php if (isset($error)): ?> 
       <div style="color:red"> You must fill out the form!</div> 
       <?php endif ?> 

試試這個它可能工作

感謝

+1

問題解決!非常感謝。 – pacmanfordinner

+0

很高興知道您的問題已得到解決。 –

0

嘗試改變empty功能與isset功能

相關問題