2017-03-23 92 views
0

在我進入它之前,我想說,這確實從我的本地服務器發送電子郵件,所以我已經設置了一切。添加此表單驗證後,它不再發送電子郵件或顯示錯誤。它只是刷新頁面。我是PHP編程的新手,所以我確定我只是有一個if語句錯誤的順序或類似的東西。聯繫表格不顯示錯誤信息或發送電子郵件

HTML

<section id="contact"> 
      <h1 class="section-header">Contact Us Directly</h1> 
      <h4 class="text-center">Have any quesitons not answered in the <span><a href="questions.php">Questions Page</a></span>.</h4> 
      <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST"> 
       <label for="fname">First Name</label> 
       <input type="text" id="fname" name="firstname" value="<?php $firstname ?>" placeholder="Your name.." tabindex="1" autofocus> 
       <span class="error"><?php $firstname_error ?></span> 
       <label for="lname">Last Name</label> 
       <input type="text" id="lname" name="lastname" value="<?php $lastname ?>" placeholder="Your last name.." tabindex="2"> 
       <span class="error"><?php $lastname_error ?></span> 
       <label for="email">Email</label> 
       <input type="text" id="email" name="email" value="<?php $email ?>" placeholder="Your email.." tabindex="3"> 
       <span class="error"><?php $email_error ?></span> 
       <label for="message">Message</label> 
       <textarea id="subject" name="message" value="<?php $message ?>" placeholder="Write something.." style="height:200px" tabindex="4"> </textarea> 
       <span class="error"><?php $message_error ?></span> 
       <input type="submit" value="Submit" tabindex="5"> 
       <span class="success"><?php $success ?></span> 
      </form> 
     </section> 

PHP

<?php 

$firstname_error = $lastname_error = $email_error = $message_error = ""; 
$firstname = $lastname = $email = $message = $success = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["firstname"])) { 
    $firstname_error = "First name is required"; 
    } else { 
    $firstname = test_input($_POST["firstname"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) { 
     $firstname_error = "Only letters and white space allowed"; 
    } 
    } 

    if (empty($_POST["lastname"])) { 
    $lastname_error = "Last name is required"; 
    } else { 
    $lastname = test_input($_POST["lastname"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) { 
     $lastname_error = "Only letters and white space allowed"; 
    } 
    } 

    if (empty($_POST["email"])) { 
    $email_error = "Email is required"; 
    } else { 
    $email = test_input($_POST["email"]); 
    // check if e-mail address is well-formed 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
     $email_error = "Invalid email format"; 
    } 
    } 

    if (empty($_POST["message"])) { 
    $message = ""; 
    } else { 
    $message = test_input($_POST["message"]); 
    } 


if ($firstname_error == '' and $lastname_error == '' and $email_error == '' and $message_error == ''){ 
     $message_body = ''; 
     unset($_POST['submit']); 
     foreach ($_POST as $key => $value){ 
      $message_body .= "$key: $value\n"; 
     } 

     $EmailFrom = localhost; 
     $EmailTo = "[email protected]"; 
     $Subject = "New Order From tabletscreenfixer.com"; 
     if (mail($EmailTo, $Subject, $message)){ 
      $success = "Message sent, thank you for contacting us!"; 
      $firstname = $lastname = $email = $message = ''; 
     } 
    } 

} 



function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 

?> 

回答

1

添加一些調試。例如,在任何地方你有一個錯誤,增加一個錯誤計數器。事情是這樣的:

if (empty($_POST["email"])) { 
    $email_error = "Email is required"; 
    $errors++; 
} else { 
    $email = test_input($_POST["email"]); 
    // check if e-mail address is well-formed 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
    $email_error = "Invalid email format"; 
    } 
} 

然後,到了最後,而不是長,如果條件:

if ($firstname_error == '' and $lastname_error == '' and $email_error == '' and $message_error == ''){ 

您可以使用類似:

if ($errors == 0){ 

這表明沒有錯誤。然後,裏面的是,如果當您嘗試發送郵件,檢查是否存在故障:

if (mail($EmailTo, $Subject, $message)){ 
     $success = "Message sent, thank you for contacting us!"; 
     $firstname = $lastname = $email = $message = ''; 
    } 
    else { 
     echo "The mail command failed!!"; 
    } 

最後,給自己某種指標的,有錯誤,只爲您的測試階段。您可以刪除這個別人(甚至加上優秀的設計,並保持它):

if ($errors == 0){ 

// snip - lines of code in here removed to keep this snippet readable. this is your test and send mail logic. 

} 
else { 
    echo "You've got $errors errors! You need to correct them!"; 
} 

有了這些變化,你應該能夠很快找到你的問題。正如我所說的,一旦完成調試,還可以刪除一些代碼(如echo語句)。

祝你好運!

+0

這是一些很好的建議!我有一個問題,但是當我選擇提交按鈕,它只是重置頁面,這是因爲我設置我的表單的方式? – cosmichero2025

+0

form action =決定當你點擊提交按鈕時會發生什麼。在這種情況下,它會再次調用相同的頁面並重新加載它。每次你點擊它,Web服務器被再次調用來渲染頁面,並再次執行所有的HTML和PHP代碼... –

2

你的代碼正在乞求一些改進@R史密斯提到;不過這個版本有效;我在我的電腦上測試過。 check the image

<?php 

    $firstname_error = $lastname_error = $email_error = $message_error = ""; 
    $firstname = $lastname = $email = $message = $success = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["firstname"])) { 
    $firstname_error = "First name is required"; 
    } else { 
    $firstname = test_input($_POST["firstname"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) { 
     $firstname_error = "Only letters and white space allowed"; 
    } 
} 

if (empty($_POST["lastname"])) { 
    $lastname_error = "Last name is required"; 
} else { 
    $lastname = test_input($_POST["lastname"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) { 
     $lastname_error = "Only letters and white space allowed"; 
    } 
} 

if (empty($_POST["email"])) { 
    $email_error = "Email is required"; 
} else { 
    $email = test_input($_POST["email"]); 
    // check if e-mail address is well-formed 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
     $email_error = "Invalid email format"; 
    } 
} 

if (empty($_POST["message"])) { 
    $message = ""; 
} else { 
    $message = test_input($_POST["message"]); 
} 


if ($firstname_error == '' and $lastname_error == '' and $email_error == '' and $message_error == ''){ 
    $message_body = ''; 
    unset($_POST['submit']); 
    // var_dump($_POST); exit(); 
    foreach ($_POST as $key => $value){ 
     $message_body .= "$key: $value\n"; 
    } 
    $EmailFrom = "[email protected]"; 
    $EmailTo = "tabletscreenfixer.com";//-> the message will be sent to this address if you have configure mail stuff well 
    $Subject = "New Order From tabletscreenfixer.com"; 
    if (mail($EmailTo, $Subject, $message)){ 
     $success = "Message sent, thank you for contacting us!"; 
     $firstname = $lastname = $email = $message = ''; 
    }else{ 
     echo "Failure"; 
    } 
}else{ 
    echo "Failure 2"; 
    } 
} 
    function test_input($data) { 
     $data = trim($data); 
     $data = stripslashes($data); 
     $data = htmlspecialchars($data); 
     return $data; 
    } 
?> 

<html> 
<head> 
    <title>Test</title> 
</head> 
<body> 
<section id="contact"> 
    <h1 class="section-header">Contact Us Directly</h1> 
    <h4 class="text-center">Have any quesitons not answered in the <span><a href="questions.php">Questions Page</a></span>.</h4> 
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST"> 
     <label for="fname">First Name</label> 
     <input type="text" id="fname" name="firstname" value="<?php echo $firstname ?>" placeholder="Your name.." tabindex="1" autofocus> 
     <span class="error"><?php echo $firstname_error ?></span> 
     <label for="lname">Last Name</label> 
     <input type="text" id="lname" name="lastname" value="<?php echo $lastname ?>" placeholder="Your last name.." tabindex="2"> 
     <span class="error"><?php echo $lastname_error ?></span> 
     <label for="email">Email</label> 
     <input type="text" id="email" name="email" value="<?php echo $email ?>" placeholder="Your email.." tabindex="3"> 
     <span class="error"><?php echo $email_error ?></span> 
     <label for="message">Message</label> 
     <textarea id="subject" name="message" value="<?php echo $message ?>" placeholder="Write something.." style="height:200px" tabindex="4"> </textarea> 
     <span class="error"><?php echo $message_error ?></span> 
     <input type="submit" value="Submit" tabindex="5"> 
     <span class="success"><?php $success ?></span> 
    </form> 
</section> 
</body> 
</html> 

編輯:在輸入值屬性缺少回聲; 希望它有幫助;

+0

嘿抱歉花了這麼長時間來回復我的權力anywayyyyy。當我使用你的代碼而不是頁面只是重新加載,現在什麼都沒有發生,它給了我一個403錯誤Acess禁止! – cosmichero2025

+0

頁面重新加載,因爲它將請求發送給自己;你可能需要刪除這一行:unset($ _ POST ['submit']); –

+0

即使在此之後,它仍然重新加載我只是不知道爲什麼它這樣做 – cosmichero2025

相關問題