php
2016-07-04 127 views -2 likes 
-2

你好,所以這可能是一個愚蠢的問題,但我是一個初學者在PHP和想做一個電子郵件形式與驗證,當我點擊按鈕提交它重新加載頁面發送郵件即使不是所有的輸入都完成,並且點擊按鈕後,它表示沒有任何表單已經完成。我需要Ajax纔不會發生這種情況嗎? 這裏是你可能會更好地理解代碼: PHP的:Php電子郵件表單驗證,驗證不起作用

<?php 
$nameErr = $emailErr = $messageErr = ""; 

$to = '[email protected]'; 
$subject = 'Email form test'; 

$name = $_POST['name']; 
$email = $_POST['email']; 
$message = $_POST['message']; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (isset($_POST["name"])) { 
     $nameErr = "* Please enter your name"; 
    } else { 
     $name = test_input($_POST["name"]); 
    } 

    if (isset($_POST["email"])) { 
     $emailErr = "* Please enter your email"; 
    } else { 
     $email = test_input($_POST["email"]); 
    } 

    if (isset($_POST['message'])) { 
     $messageErr = "* Please enter a message"; 
    } else { 
     $message = test_input($_POST["message"]); 
    } 

$message = <<<EMAIL 

$name 
$email 
$message 

EMAIL; 

$header = '$email'; 

if ($_POST['submit']){ 
mail($to , $subject , $message , $header); 
$feedback = 'Your email has been sent.'; 
} 
} 

?> 

HTML:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
    <div class="name"> 
     <input type="text" name="name" id="name" value="" placeholder="Name" size="50"> 
     <span class="error"><?php echo $nameErr;?></span> 
    </div> 
<br> 
    <div class="email"> 
     <input type="text" name="email" id="email" value="" placeholder="E-Mail" size="50"> 
     <span class="error"><?php echo $emailErr;?></span> 
    </div> 
<br> 
    <div class="message"> 
     <textarea name="message" id="message" placeholder="Enter your message" rows="6"></textarea> 
     <span class="error"><?php echo $messageErr;?></span> 
    </div> 
<br> 
    <div class="submit"> 
     <input type="submit" name="submit" class="button special"> 
    </div> 
</form>  
<p id="feedback"><?php echo $feedback; ?></p> 

謝謝。

+2

「*無論窗體處於什麼狀態,我都會調用mail() - 爲什麼它發送電子郵件,無論窗體處於什麼狀態?*」。不,你不需要AJAX,只是當你不想發送郵件時不要調用發送郵件功能。 – TessellatingHeckler

回答

0

請參考isset手冊。它確定是否設置了變量並且不是NULL。

//Pay attention to ! (negation) 
if (!isset($_POST["email"])) { 
    $emailErr = "* Please enter your email"; 
} 

驗證您可以使用電子郵件:

if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) { 
    $emailErr = "Invalid email format"; 
} 
1

我已經rewritting你的代碼了一下,我覺得應該這樣工作。

<?php 

$errors = array(); 
$to = '[email protected]'; 
$subject = 'Email form test'; 

$requiredFields = array('name', 'email', 'message'); 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    foreach ($required as $field) { 
     if (!array_key_exists($field, $_POST)) { 
      $errors[$field] = '* '.$field.' cannot be empty'; 
     } else if (!test_input($_POST[$field])) { 
      $errors[$field] = '* '.$field.' does not meet the validation'; 
     } 
    } 

    $message = $name."\n".$email."\n".$message."\n"; 
    $header = '$email'; 
    if (isset($_POST['submit'])) { 
     mail($to , $subject , $message , $header); 
    } 
    $feedback = 'Your email has been sent.'; 
} 
?> 
相關問題