2016-11-07 317 views
-2

我已經使用一些PHP代碼創建了一個簡單的聯繫表單,並允許它在某些情況下給出錯誤,如字段爲空或內容的領域不匹配某些標準。 。 我已經應用了統一標準的電子郵件輸入框(如必須包含「@」符號但是,即使所有正確的標準都匹配了,我的PHP仍然拋出和錯誤PHP聯繫表單輸入字段錯誤,但不知道爲什麼

下面是代碼:

<?php 
 
if(isset($_POST['email'])){ 
 

 
\t // Here is the email to information 
 
\t $email_to ="[email protected]"; 
 
\t $email_subject ="example.co.uk contact form"; 
 
\t $email_from ="Website Contact Form"; 
 
\t 
 
\t // Error Code 
 
\t 
 
\t function died($error) { 
 
\t \t echo 'Sorry, there is a problem with the form you submitted. '; 
 
\t \t echo 'These errors appear below.<br/><br/>'; 
 
\t \t echo $error. '<br/><br/>'; 
 
\t \t echo 'Please go back and fix these errors.'; 
 
\t \t die(); 
 
\t \t } 
 
\t \t 
 
\t // Validation 
 
\t 
 
\t \t if(!isset($_POST['name']) || 
 
\t \t !isset($_POST['email']) || 
 
\t \t !isset($_POST['comments'])){ 
 
\t \t \t died('All fields must be filled out.'); 
 
\t \t \t } \t 
 
\t \t 
 
\t \t $name = $_POST['name']; 
 
\t \t $email = $_POST['email']; 
 
\t \t $comments = $_POST['comments']; 
 
\t \t 
 
\t \t $error_message = ""; 
 
\t \t $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z] 
 
\t \t {2,4}$/'; 
 
\t \t if(!preg_match($email_exp, $email)){ 
 
\t \t \t $error_message .= 'The Email Address you entered does not appear to be valid.'; 
 
\t \t \t } 
 
\t \t 
 
\t \t $string_exp = "/^[A-Za-z.'-]+$/"; 
 
\t \t if (!preg_match($string_exp, $name)){ 
 
\t \t \t $error_message .= 'The Name you entered does not appear to be valid.'; 
 
\t \t \t } 
 
\t \t if (strlen($comments) < 2){ 
 
\t \t \t $error_message .= 'The Comments you entered do not appear to be valid.<br/>'; 
 
\t \t \t } 
 
\t \t if (strlen($error_message) > 0){ 
 
\t \t \t died($error_message); 
 
\t \t \t } 
 
\t \t \t $email_message = "Form Details Below. \n\n"; 
 
\t \t \t 
 
\t \t function clean_string($string){ 
 
\t \t $bad = array("content-type", "bcc:", "to:", "cc:", "href"); 
 
\t \t return str_replace($bad, "", $string); 
 
\t \t } 
 
\t \t 
 
\t \t $email_message .= "Name:" . clean_string($name) . "\n"; 
 
\t \t $email_message .= "Email:" . clean_string($email) . "\n"; 
 
\t \t $email_message .= "Comments:" . clean_string($comments) . "\n"; 
 
\t \t 
 
\t \t 
 
\t \t // Create Email Headers 
 
\t \t $headers = 'From: ' .$email_from . "\r\n". 'Reply-To:' . $email. "\r\n" . 
 
\t \t 'X-Mailer: PHP/' . phpversion(); 
 
\t \t @mail($email_to, $email_subject, $email_message, $headers); 
 
\t \t 
 
?> 
 
<html> 
 
<!-- success message goes here--> 
 
Thank You for contacting us, we will be in touch shortly. <br/> 
 
Please Click <a href="contact.html">here<a/> to go back to the contact page. 
 
</html> 
 
<?php } ?>

沒有人明白爲什麼它仍然給我的錯誤信息?

+0

你會得到什麼錯誤? –

+0

用於調試,請從@mail($ ema)中刪除'@'(@ ema)@隱藏可能有助於調試的錯誤 –

+0

您需要顯示錯誤.. –

回答

0

可以使用自舉框架,以更好的樣子

就這樣它看起來: bootstrap contact form

您contact.php應包含:

<?php 
if (isset($_POST["submit"])) { 
    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $message = $_POST['message']; 
    $human = intval($_POST['human']); 
    $from = 'Demo Contact Form'; 
    $to = "[email protected]"; 
    $subject = 'Email from Tropilac.com'; 

    $body = "From: $name\n E-Mail: $email\n Message:\n $message"; 

    // Check if name has been entered 
    if (!$_POST['name']) { 
     $errName = 'Please enter your name'; 
    } 

    // Check if email has been entered and is valid 
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { 
     $errEmail = 'Please enter a valid email address'; 
    } 

    //Check if message has been entered 
    if (!$_POST['message']) { 
     $errMessage = 'Please enter your message'; 
    } 
    //Check if simple anti-bot test is correct 
    if ($human !== 5) { 
     $errHuman = 'Your anti-spam is incorrect'; 
    } 

    // If there are no errors, send the email 
    if (!$errName && !$errEmail && !$errMessage && !$errHuman) { 
     if (mail ($to, $subject, $body, $from)) { 
      $result='<div class="alert alert-success">Thank You! I will be in touch</div>'; 
     } else { 
      $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>'; 
     } 
    } 
} 
?> 

和HTML應該是:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Bootstrap Contact Form</title> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> 
</head> 
<body> 
<div class="container"> 
    <div class="row"> 
     <div class="col-md-6 col-md-offset-3"> 
      <h1 class="page-header text-center">Contact Form Example</h1> 
      <form class="form-horizontal" role="form" method="post" action="index.php"> 
       <div class="form-group"> 
        <label for="name" class="col-sm-2 control-label">Name</label> 
        <div class="col-sm-10"> 
         <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>"> 
         <?php echo "<p class='text-danger'>$errName</p>";?> 
        </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="<?php echo htmlspecialchars($_POST['email']); ?>"> 
         <?php echo "<p class='text-danger'>$errEmail</p>";?> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label for="message" class="col-sm-2 control-label">Message</label> 
        <div class="col-sm-10"> 
         <textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea> 
         <?php echo "<p class='text-danger'>$errMessage</p>";?> 
        </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="Your Answer"> 
         <?php echo "<p class='text-danger'>$errHuman</p>";?> 
        </div> 
       </div> 
       <div class="form-group"> 
        <div class="col-sm-10 col-sm-offset-2"> 
         <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary"> 
        </div> 
       </div> 
       <div class="form-group"> 
        <div class="col-sm-10 col-sm-offset-2"> 
         <?php echo $result; ?> 
        </div> 
       </div> 
      </form> 
     </div> 
    </div> 
</div> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> 
</body> 
</html> 

所有源都可以找到here and here