2016-07-20 81 views
-1

我在發送PHP表單後生成成功或錯誤消息時遇到問題。電子郵件到達正確,但之後的空白屏幕是一個問題。php聯繫表單錯誤和成功消息丟失

<?php 
     if (isset($_POST["submit"])) { 
      $name = $_POST['name']; 
      $email = $_POST['email']; 
      $puhelin = $_POST['puhelin']; 
      $ajankohta = $_POST['ajankohta']; 
      $message = $_POST['message']; 
      $human = intval($_POST['human']); 
      $from = 'x.fi'; 
      $to = '[email protected]'; 
      $subject = 'x '; 

      $body ="Nimi: $name\n E-Mail: $email\n Viesti:\n $message\n Ajankohta:\n $ajankohta\n Puhelin:\n $puhelin"; 

      if (!$_POST['name']) { 
       $errName = 'Annan nimesi'; 
      } 
       if (!$_POST['ajankohta']) { 
       $errName = 'Anna ajankohta'; 
      } 

      if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { 
       $errEmail = 'Ole hyvä ja anna oikea osoite'; 
      } 
      if (!$_POST['puhelin']) { 
       $errName = 'Anna puhelinnumero'; 
      } 

      if (!$_POST['message']) { 
       $errMessage = 'Kirjoita viesti'; 
      } 
      if ($human !== 5) { 
       $errHuman = 'Your anti-spam is incorrect'; 
      } 

在我看來,這有一個問題,如果。

 if (mail ($to, $subject, $body, $from)) { 
      $result='Success'; 
     } else { 
      $result='Fail'; 
     } 
    } 

    ?> 

這是形式的標記:

<div class="contact-form"> 
    <div class="container"> 
     <div class="row"> 
      <div class="col-md-6 col-md-offset-3"> 
       <h2 class="page-header text-center">Yhteyslomake</h2> 
      <form class="form-horizontal" role="form" method="post" action="index.php"> 
       <div class="form-group"> 
        <label for="name" class="col-sm-2 control-label">Nimesi</label> 
        <div class="col-sm-10"> 
         <input type="text" class="form-control" id="name" name="name" placeholder="Nimesi" value=""> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label for="email" class="col-sm-2 control-label">Email</label> 
        <div class="col-sm-10"> 
         <input type="email" class="form-control" id="email" name="email" placeholder="[email protected]" value=""> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label for="puhelin" class="col-sm-2 control-label">Puhelin</label> 
        <div class="col-sm-10"> 
         <input type="" class="form-control" id="puhelin" name="puhelin" placeholder="+358" value=""> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label for="message" class="col-sm-2 control-label">Viesti</label> 
        <div class="col-sm-10"> 
         <textarea class="form-control" rows="4" name="message" placeholder="Kerro juhliesi sijainti ja pieni kuvaus"></textarea> 
        </div> 
       </div> 
        <div class="form-group"> 
        <label for="ajankohta" class="col-sm-2 control-label">Ajankohta</label> 
        <div class="col-sm-10"> 
         <input type="" class="form-control" id="ajankohta" name="ajankohta" placeholder="Päivä/Kuukausi/Kellonaika" value=""> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label for="human" class="col-sm-2 control-label">2 + 3 = ?</label> 
        <div class="col-sm-10"> 
         <input type="text" class="form-control" id="human" name="human" placeholder="Vastauksesi"> 
         <p class='text-danger'></p> 
        </div> 
       </div> 
       <div class="form-group"> 
        <div class="col-sm-10 col-sm-offset-2"> 
         <input id="submit" name="submit" type="submit" value="Lähetä" class="btn btn-primary"> 
        </div> 
       </div> 
       <div class="form-group"> 
        <div class="col-sm-10 col-sm-offset-2"> 
        </div> 
       </div> 
      </form> 
     </div> 
    </div> 
</div> 

感謝您的幫助!

+0

打開error_reporting第一個 –

+3

你在迴應'$ result',還是隻是分配它?如果你不告訴它,它不會輸出任何東西。 – aynber

+0

分享表單標記。 –

回答

1
Assign all error message in single variable 

Ex: $error = array(); 
if($name == '') { 
$error[name] = 'Name is invalid'; 
} 

if($email == '') { 
$error[email] = 'Email is invalid'; 
} 

$cErrors = count($errors); 

if ($cErrors > 0) { 
    echo '<p>Total errors: ' . $cErrors . '</p>'; 
    $error = '<p>ERROR: Please fill in all required fields!</p>'; 
    echo "<ul>"; 
    echo implode("\n", $error); 
    echo "</ul>"; 
}