2016-12-16 87 views
0

我的聯繫表格不工作,沒有任何反應。我的電子郵件聯繫表不能正常工作

它重定向到contact.php頁面,但點擊按鈕什麼也沒有發生,甚至沒有顯示驗證錯誤。問題是什麼?

<form method="post" id="contact-form" action="contact.php"> 
    <h2>Send us your inquiry</h2> 
    <input name="name" id="name" type="text" placeholder="Name"> 
    <input name="mail" id="mail" type="text" placeholder="Email"> 
    <input name="tel-number" id="tel-number" type="text" placeholder="Phone"> 
    <textarea name="comment" id="comment" placeholder="Message"></textarea> 
    <input type="submit" name="submit" id="submit_contact" value="Send Message"> 
    <div id="msg" class="message"></div> 
</form> 

<?php 

/* ========================== Define variables ========================== */ 

#Your e-mail address 
define("__TO__", "[email protected]"); 

#Message subject 
define("__SUBJECT__", "examples.com = From:"); 

#Success message 
define('__SUCCESS_MESSAGE__', "Your message has been sent. Thank you!"); 

#Error message 
define('__ERROR_MESSAGE__', "Error, your message hasn't been sent"); 

#Messege when one or more fields are empty 
define('__MESSAGE_EMPTY_FILDS__', "Please fill out all fields"); 

/* ======================== End Define variables ======================== */ 

//Send mail function 
function send_mail($to,$subject,$message,$headers){ 
    if(@mail($to,$subject,$message,$headers)){ 
     echo json_encode(array('info' => 'success', 'msg' => __SUCCESS_MESSAGE__)); 
    } else { 
     echo json_encode(array('info' => 'error', 'msg' => __ERROR_MESSAGE__)); 
    } 
} 

//Check e-mail validation 
function check_email($email){ 
    if([email protected]("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){ 
     return false; 
    } else { 
     return true; 
    } 
} 

//Get post data 
if(isset($_POST['name']) and isset($_POST['mail']) and isset($_POST['comment'])){ 
    $name = $_POST['name']; 
    $mail = $_POST['mail']; 
    $website = $_POST['website']; 
    $comment = $_POST['comment']; 

    if($name == '') { 
     echo json_encode(array('info' => 'error', 'msg' => "Please enter your name.")); 
     exit(); 
    } else if($mail == '' or check_email($mail) == false){ 
     echo json_encode(array('info' => 'error', 'msg' => "Please enter valid e-mail.")); 
     exit(); 
    } else if($comment == ''){ 
     echo json_encode(array('info' => 'error', 'msg' => "Please enter your message.")); 
     exit(); 
    } else { 
     //Send Mail 
     $to = __TO__; 
     $subject = __SUBJECT__ . ' ' . $name; 
     $message = ' 
      <html> 
      <head> 
       <title>Mail from '. $name .'</title> 
      </head> 
      <body> 
       <table style="width: 500px; font-family: arial; font-size: 14px;" border="1"> 
       <tr style="height: 32px;"> 
        <th align="right" style="width:150px; padding-right:5px;">Name:</th> 
        <td align="left" style="padding-left:5px; line-height: 20px;">'. $name .'</td> 
       </tr> 
       <tr style="height: 32px;"> 
        <th align="right" style="width:150px; padding-right:5px;">E-mail:</th> 
        <td align="left" style="padding-left:5px; line-height: 20px;">'. $mail .'</td> 
       </tr> 
       <tr style="height: 32px;"> 
        <th align="right" style="width:150px; padding-right:5px;">Website:</th> 
        <td align="left" style="padding-left:5px; line-height: 20px;">'. $website .'</td> 
       </tr> 
       <tr style="height: 32px;"> 
        <th align="right" style="width:150px; padding-right:5px;">Comment:</th> 
        <td align="left" style="padding-left:5px; line-height: 20px;">'. $comment .'</td> 
       </tr> 
       </table> 
      </body> 
      </html> 
     '; 

     $headers = 'MIME-Version: 1.0' . "\r\n"; 
     $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; 
     $headers .= 'From: ' . $mail . "\r\n"; 

     send_mail($to,$subject,$message,$headers); 
    } 
} else { 
    echo json_encode(array('info' => 'error', 'msg' => __MESSAGE_EMPTY_FILDS__)); 
} 
?> 

回答

0

這種工作適合我。我曾經是它第一次顯示錯誤。你不清理$ _POST。你不需要採取行動,因爲它在同一頁面上。

這裏是一個少許修改,對我工作的版本:

<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title></title> 
    </head> 
    <body> 
<?php 

/* ========================== Define variables ========================== */ 

#Your e-mail address 
define("__TO__", "[email protected]"); 

#Message subject 
define("__SUBJECT__", "examples.com = From:"); 

#Success message 
define('__SUCCESS_MESSAGE__', "Your message has been sent. Thank you!"); 

#Error message 
define('__ERROR_MESSAGE__', "Error, your message hasn't been sent"); 

#Messege when one or more fields are empty 
define('__MESSAGE_EMPTY_FILDS__', "Please fill out all fields"); 

/* ======================== End Define variables ======================== */ 

//Send mail function 
function send_mail($to,$subject,$message,$headers){ 
    if(@mail($to,$subject,$message,$headers)){ 
     echo json_encode(array('info' => 'success', 'msg' => __SUCCESS_MESSAGE__)); 
    } else { 
     echo json_encode(array('info' => 'error', 'msg' => __ERROR_MESSAGE__)); 
    } 
} 

//Check e-mail validation 
function check_email($email){ 
    if([email protected]("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){ 
     return false; 
    } else { 
     return true; 
    } 
} 
if (!(isset($_POST['name']) && isset($_POST['mail']) && isset($_POST['comment']))) 
{ 
    ?> 
    <form method="post" id="contact-form"> 
    <h2>Send us your inquiry</h2> 
     <input name="name" id="name" type="text" placeholder="Name"> 
     <input name="mail" id="mail" type="text" placeholder="Email"> 
     <input name="tel-number" id="tel-number" type="text" placeholder="Phone"> 
     <textarea name="comment" id="comment" placeholder="Message"></textarea> 
     <input type="submit" name="submit" id="submit_contact" value="Send Message"> 
     <div id="msg" class="message"></div> 
    </form>   
    <?php 
} 
else 
{ 
    $data = filter_var_array($_POST, FILTER_SANITIZE_STRING); 

    $name = $data['name']; 
    $mail = $data['mail']; 
    $website = $data['website']; 
    $comment = $data['comment']; 

    if($name == '') { 
     echo json_encode(array('info' => 'error', 'msg' => "Please enter your name.")); 
     exit(); 
    } else if($mail == '' or check_email($mail) == false){ 
     echo json_encode(array('info' => 'error', 'msg' => "Please enter valid e-mail.")); 
     exit(); 
    } else if($comment == ''){ 
     echo json_encode(array('info' => 'error', 'msg' => "Please enter your message.")); 
     exit(); 
    } else { 
     //Send Mail 
     $to = __TO__; 
     $subject = __SUBJECT__ . ' ' . $name; 
     $message = ' 
     <html> 
     <head> 
      <title>Mail from '. $name .'</title> 
     </head> 
     <body> 
      <table style="width: 500px; font-family: arial; font-size: 14px;" border="1"> 
      <tr style="height: 32px;"> 
       <th align="right" style="width:150px; padding-right:5px;">Name:</th> 
       <td align="left" style="padding-left:5px; line-height: 20px;">'. $name .'</td> 
      </tr> 
      <tr style="height: 32px;"> 
       <th align="right" style="width:150px; padding-right:5px;">E-mail:</th> 
       <td align="left" style="padding-left:5px; line-height: 20px;">'. $mail .'</td> 
      </tr> 
      <tr style="height: 32px;"> 
       <th align="right" style="width:150px; padding-right:5px;">Website:</th> 
       <td align="left" style="padding-left:5px; line-height: 20px;">'. $website .'</td> 
      </tr> 
      <tr style="height: 32px;"> 
       <th align="right" style="width:150px; padding-right:5px;">Comment:</th> 
       <td align="left" style="padding-left:5px; line-height: 20px;">'. $comment .'</td> 
      </tr> 
      </table> 
     </body> 
     </html> 
     '; 

     $headers = 'MIME-Version: 1.0' . "\r\n"; 
     $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; 
     $headers .= 'From: ' . $mail . "\r\n"; 

     send_mail($to,$subject,$message,$headers); 
    } 
} 
?> 
    </body> 
</html> 
+0

BT我的表單代碼是在HTML頁面...我該如何使用呢? – Siddhu

+0

我發給你的網頁是一些嵌入式php的html代碼。您需要確保您的系統上正確安裝了php。 –

+0

你不能得到我說的......我的表單頁面是contactus.html,編碼頁面是contact.php .....所以我不能在html頁面中使用php代碼... – Siddhu