2013-12-19 60 views
-2

我想使PHP的形式,它是給我發送成功的消息,但我沒有收到任何郵件,嘗試了一切,但沒有用。 繼承人的我主要的PHP代碼:PHP表單不工作,雖然做的一切正確

<?php 
error_reporting(E_ALL^E_NOTICE); // hide all basic notices from PHP 

//If the form is submitted 
if(isset($_POST['submitted'])) { 

    // require a name from user 
    if(trim($_POST['contactName']) === '') { 
     $nameError = 'Forgot your name!'; 
     $hasError = true; 
    } else { 
     $name = trim($_POST['contactName']); 
    } 

    // require a name from user 
    if(trim($_POST['phone']) === '') { 
     $phoneError = 'Forgot your contact!'; 
     $hasError = true; 
    } else { 
     $phone = trim($_POST['phone']); 
    } 

    // need valid email 
    if(trim($_POST['email']) === '') { 
     $emailError = 'Forgot to enter in your e-mail address.'; 
     $hasError = true; 
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) { 
     $emailError = 'You entered an invalid email address.'; 
     $hasError = true; 
    } else { 
     $email = trim($_POST['email']); 
    } 

    // we need at least some content 
    if(trim($_POST['comments']) === '') { 
     $commentError = 'You forgot to enter a message!'; 
     $hasError = true; 
    } else { 
     if(function_exists('stripslashes')) { 
      $comments = stripslashes(trim($_POST['comments'])); 
     } else { 
      $comments = trim($_POST['comments']); 
     } 
    } 

    // upon no failure errors let's email now! 
    if(!isset($hasError)) { 

     $emailTo = '[email protected]'; 
     $subject = 'MATHESON FIRST AID: ENQUIRY FORM SUBMISSION '.$name; 
     $sendCopy = trim($_POST['sendCopy']); 
     $body = "Name: $name \n\nEmail: $email \n\nComments: $comments"; 
     $headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; 

     mail($emailTo, $subject, $body, $headers); 

     // set our boolean completion value to TRUE 
     $emailSent = true; 
    } 
} 
?> 

,這裏是我的HTML代碼,請注意,我已經做了腳本,並在同一頁上的形式,因此,嵌入在HTML爲好,在這裏不用了表單HTML:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<title>hussain</title> 

<link rel="stylesheet" href="css/style.css" type="text/css" > 
    <script type="text/javascript" src="js/jquery-1.5.2.js" ></script> 
</head> 

<body> 
     <div class="wrapper_main"> 

      <?php if(isset($emailSent) && $emailSent == true) { 
       include 'success.html'; 
      } else { ?> 

      <div id="content"> 
       <section class="section_right"> 
        <img src="images/right2.png" alt="" title="" /> 
       </section> 
       <div class="main_content"> 
        <h1>Enquiry Form</h1><br/> 
        <?php if(isset($hasError) || isset($captchaError)) { ?> 
         <p class="alert">Error submitting the form</p> 
        <?php } ?> 
        <div class="form_wrap2"> 
         <form id="contact-us" action="enquiry.php" method="post"> 
          <fieldset> 
           <label for="name">Name:</label> 
           <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" placeholder="Enter your name" /> 
           <?php if($nameError != '') { ?> 
           <br /><span class="error"><?php echo $nameError;?></span> 
           <?php } ?> 

           <label for="title">Company</label> 
           <input name="title" type="text" id="company" placeholder="Enter your company name" /> 

           <label for="address">Address:</label> 
           <input name="address" type="text" id="address" placeholder="Enter your address" /> 

           <label for="tel">Telephone:</label> 
           <input type="text" name="phone" id="phone" value="<?php if(isset($_POST['phone'])) echo $_POST['phone'];?>" placeholder="Enter your telephone number" /> 
           <?php if($phoneError != '') { ?> 
           <br /><span class="error"><?php echo $phoneError;?></span> 
           <?php } ?> 

           <label for="email">Email:</label> 
           <input type="email" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" placeholder="Enter your email address" /> 
           <?php if($emailError != '') { ?> 
           <br /><span class="error"><?php echo $emailError;?></span> 
           <?php } ?> 

           <label for="business">Type of Business:</label> 
           <input name="bussiness" type="text" id="business" placeholder="Enter type of business" /> 

           <label for="enquiry">Enquiry:</label> 
           <textarea name="comments" id="commentsText" id="comments" placeholder="Enter comments here" ><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea> 
           <?php if($commentError != '') { ?> 
           <br /><span class="error"><?php echo $commentError;?></span> 
           <?php } ?> 

           <input type="reset" value="Reset" /> 
           <input type="submit" value="Submit" /> 
           <input type="hidden" name="submitted" id="submitted" value="true" /> 

          </fieldset> 
         </form> 
        </div> 
        <?php } ?> 
       </div> 
      </div> 
     </div> 
    </div> 
    <!-- content end --> 

    </div> 
</div> 
<script type="text/javascript"> 
    <!--//--><![CDATA[//><!-- 
    $(document).ready(function() { 
     $('form#contact-us').submit(function() { 
      $('form#contact-us .error').remove(); 
      var hasError = false; 
      $('.requiredField').each(function() { 
       if($.trim($(this).val()) == '') { 
        var labelText = $(this).prev('label').text(); 
        $(this).parent().append('<span class="error">Your forgot to enter your '+labelText+'.</span>'); 
        $(this).addClass('inputError'); 
        hasError = true; 
       } else if($(this).hasClass('email')) { 
        var emailReg = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/; 
        if(!emailReg.test($.trim($(this).val()))) { 
         var labelText = $(this).prev('label').text(); 
         $(this).parent().append('<span class="error">Sorry! You\'ve entered an invalid '+labelText+'.</span>'); 
         $(this).addClass('inputError'); 
         hasError = true; 
        } 
       } 
      }); 
      if(!hasError) { 
       var formInput = $(this).serialize(); 
       $.post($(this).attr('action'),formInput, function(data){ 
        $('form#contact-us').slideUp("fast", function() {     
         $(this).before('<p class="tick"><strong>Thanks!</strong> Your email has been delivered. Huzzah!</p>'); 
        }); 
       }); 
      } 

      return false; 
     }); 
    }); 
    //-->!]]> 
</script> 
</body> 
</html> 
+0

你說你已經嘗試了一切? - > http://mattgemmell.com/what-have-you-tried/ – Zak

+0

而不是手動設置'$ emailSent = true;',你應該首先檢查'mail()'函數的返回值。 – jeroen

+0

不要關閉錯誤報告!閱讀:http://www.phptherightway.com/ – calumbrodie

回答

0

夫婦告示:

  1. 不要禁用的error_reporting,直到你去生產。它會泄露可能出錯的地方,或者你不應該這樣做。無論如何,您可能會想要這樣做,因此您可以在觸發錯誤的命令前加上@。

  2. 而不是trim($_POST['contactName'] == ''使用empty($_POST['contactName'])。這樣做的好處是,如果值已設置,則會自動檢查。如果您不想要空白空間作爲輸入,請同時使用兩者。您可以使用簡單的PHP內置函數filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)而不是!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email'])

  3. $headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;是一個無效的標題,因爲您指定沒有名稱。使用$headers = 'From: ' . $emailTo . "\r\n" . 'Reply-To: ' . $email;。看到有效的頭http://php.net/manual/en/function.mail.php(以paramater)

一般建議初始化腳本的變量,在它的頂部。這樣你只需要一直檢查和修剪(?)你的輸入,但只需要做一次並將它分配給一個安全變量。

1

PHP的郵件功能運行在您的服務器上的sendmail。很可能沒有安裝和/或不運行。