2015-04-04 129 views
2

我有一個表單和m的行爲是相同的頁面。 我想:爲什麼此驗證代碼無法按預期工作?

  1. 在成功提交表單顯示一個感謝的消息旁邊,在那裏檢測到錯誤
  2. 上述所有必須在同一頁面中顯示的字段
  3. 顯示錯誤消息。

我的代碼是:

<?php 
    $errors = array(); 
    if (isset($_POST["Ask_the_Question"])) { 
     $guest_name = $_POST["guest_name"]; 
     $title = $_POST["faq_title"]; 
     $description = $_POST["faq_desc"]; 
     $title = $_POST["faq_title"]; 

     /* validation */ 
     if (empty($guest_name)) { 
      $errors['guest_name'] = "Please type your name!"; 
     } 

     if(!empty($errors)){ echo '<h1 style="color: #ff0000;">Errors!</h1><h6 style="color: #ff0000;">Please check the fields which have errors below. Error hints are in Red.</h6>';} 

     if(empty($errors)){  
      echo 'Thanks, We have received your feed back'; 
     } 
    } 

    else { 
?> 
      <form action="index.php" method="post" class="booking_reference"> 
       <input type="text" name="guest_name" placeholder="Your Name" value="<?PHP if(!empty($errors)) { echo $guest_name;} ?>" /> 
       <?php if(isset($errors['guest_name'])) { echo '<span style="color: red">'.$errors['guest_name'].'</span>'; } ?> 
       <input type="email" name="guest_email" placeholder="Your email" pattern="[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,4}$" required /> 
       <input type="text" name="faq_title" placeholder="FAQ Title"/> 
       <input type="text" name="faq_desc" placeholder="FAQ Description"/> 
       <input type="submit" name="Ask_the_Question" value="Ask the Question" /> 
      </form> 
<?php 
     } 
?> 

我已經限制了驗證,並只顯示了在這個問題上的第一部分。

當我提交此表,如果沒有任何錯誤,我收到消息謝謝,我們已經收到您的反饋 這是罰款和按預期工作。

當出現錯誤/字段客人姓名爲空我在表單提交過程中收到消息錯誤! 請檢查下面有錯誤的字段。錯誤提示以紅色顯示。 這也很好。

但我的窗體正好消失當我得到上面的消息。爲什麼? 也想顯示請輸入您的姓名!旁邊的領域。

回答

4

嘗試下面的代碼。我已經刪除了其他部分,並設置了真/假標誌來檢查是否有效。

<?php 
    $errors = array(); 
    if (isset($_POST["Ask_the_Question"])) { 
     $guest_name = $_POST["guest_name"]; 
     $title = $_POST["faq_title"]; 
     $description = $_POST["faq_desc"]; 
     $title = $_POST["faq_title"]; 

     /* validation */ 
     $chkValidate = "true"; 
     if (empty($guest_name)) { 
      $errors['guest_name'] = "Please type your name!"; 
      $chkValidate = "false"; 
     } 

     if(!empty($errors)){ echo '<h1 style="color: #ff0000;">Errors!</h1><h6 style="color: #ff0000;">Please check the fields which have errors below. Error hints are in Red.</h6>'; 
      $chkValidate = "false"; 
     } 

     if($chkValidate == "true"){  
      echo 'Thanks, We have received your feed back'; 
     } 
    } 
    ?> 
      <form action="index.php" method="post" class="booking_reference"> 
       <input type="text" name="guest_name" placeholder="Your Name" value="<?php if(!empty($errors) && $chkValidate != "false") { echo $guest_name;} ?>" /> 
       <?php if(isset($errors['guest_name'])) { echo '<span style="color: red">'.$errors['guest_name'].'</span>'; } ?> 
       <input type="email" name="guest_email" placeholder="Your email" pattern="[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,4}$" required /> 
       <input type="text" name="faq_title" placeholder="FAQ Title"/> 
       <input type="text" name="faq_desc" placeholder="FAQ Description"/> 
       <input type="submit" name="Ask_the_Question" value="Ask the Question" /> 
      </form> 
<?php 

?> 
1

只是刪除else條件的原因其實你的形式也不會顯示如果$_POST["Ask_the_Question"]設置

<?php 
    $errors = array(); 
    if (isset($_POST["Ask_the_Question"])) { 
     $guest_name = $_POST["guest_name"]; 
     $title = $_POST["faq_title"]; 
     $description = $_POST["faq_desc"]; 
     $title = $_POST["faq_title"]; 

     /* validation */ 
     if (empty($guest_name)) { 
      $errors['guest_name'] = "Please type your name!"; 
     } 

     if(!empty($errors)){ echo '<h1 style="color: #ff0000;">Errors!</h1><h6 style="color: #ff0000;">Please check the fields which have errors below. Error hints are in Red.</h6>';} 

     if(empty($errors)){  
      echo 'Thanks, We have received your feed back'; 
     } 
    } 
      <form action="index.php" method="post" class="booking_reference"> 
       <input type="text" name="guest_name" placeholder="Your Name" value="<?PHP if(!empty($errors)) { echo $guest_name;} ?>" /> 
       <?php if(isset($errors['guest_name'])) { echo '<span style="color: red">'.$errors['guest_name'].'</span>'; } ?> 
       <input type="email" name="guest_email" placeholder="Your email" pattern="[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,4}$" required /> 
       <input type="text" name="faq_title" placeholder="FAQ Title"/> 
       <input type="text" name="faq_desc" placeholder="FAQ Description"/> 
       <input type="submit" name="Ask_the_Question" value="Ask the Question" /> 
      </form> 
1

的原因是在這裏:

<?php 
if (isset($_POST["Ask_the_Question"])) { 
     $guest_name = $_POST["guest_name"]; 
     $title = $_POST["faq_title"]; 
     $description = $_POST["faq_desc"]; 
     $title = $_POST["faq_title"]; 

     /* validation */ 
     if (empty($guest_name)) { 
      $errors['guest_name'] = "Please type your name!"; 
     } 

     if(!empty($errors)){ echo '<h1 style="color: #ff0000;">Errors!</h1><h6 style="color: #ff0000;">Please check the fields which have errors below. Error hints are in Red.</h6>';} 

     if(empty($errors)){  
      echo 'Thanks, We have received your feed back'; 
     } 
    } else { 
     // your form code will never be called if $_POST['Ask_the_Question'] is set 

做你想實現你可能想什麼代之以做這樣的事情:

<?php 
    $errors = array(); 
    if (isset($_POST["Ask_the_Question"])) { 
     $guest_name = $_POST["guest_name"]; 
     $title = $_POST["faq_title"]; 
     $description = $_POST["faq_desc"]; 
     $title = $_POST["faq_title"]; 

     /* validation */ 
     if (empty($guest_name)) { 
      $errors['guest_name'] = "Please type your name!"; 
     } 

     if(!empty($errors)){ echo '<h1 style="color: #ff0000;">Errors!</h1><h6 style="color: #ff0000;">Please check the fields which have errors below. Error hints are in Red.</h6>';} 
    } 

    if(empty($errors)){  
      echo 'Thanks, We have received your feed back'; 
     } else { ?> 
      <form action="index.php" method="post" class="booking_reference"> 
       <input type="text" name="guest_name" placeholder="Your Name" value="<?PHP if(!empty($errors)) { echo $guest_name;} ?>" /> 
       <?php if(isset($errors['guest_name'])) { echo '<span style="color: red">'.$errors['guest_name'].'</span>'; } ?> 
       <input type="email" name="guest_email" placeholder="Your email" pattern="[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,4}$" required /> 
       <input type="text" name="faq_title" placeholder="FAQ Title"/> 
       <input type="text" name="faq_desc" placeholder="FAQ Description"/> 
       <input type="submit" name="Ask_the_Question" value="Ask the Question" /> 
      </form> 
      <?php 

     } 
    } 

?> 
1

其他答案很好,但只是爲了澄清會發生什麼。

但是當我得到上面的消息時,我的表單就消失了。爲什麼?

你的形式消失了,因爲如果你通過第一如果你不能讓你的其他

if (isset($_POST["Ask_the_Question"])) { 
    ... 
} else { 
    xxx; 
} 

,如果你想看到你的表格,你必須把它放在某個地方,這可以顯示像elseif(有更多的限制),或IFS內部或外部的意思。

if (isset($_POST["Ask_the_Question"]) && empty($errors)) { 
    ... 
} elseif (isset($_POST["Ask_the_Question"]) && !empty($errors)) { 
    ... 
} else { 
    ... 
} 

另外我想表明,請輸入您的姓名!在領域旁邊。

要顯示所有錯誤,您可以使用例如。在任何你想展示給他們的地方。

foreach ($errors as &$error) { 
    echo "Error: $error<br />\n"; 
} 

順便說一句empty(); function

+1

感謝您的解釋.. – 2015-05-12 16:47:22

相關問題