2015-06-17 69 views
3

我是PHP新手,因此我不確定代碼出了什麼問題。我已經在Internet Explorer,FireFox,Chrome和Safari中測試了這個表單,它在I.E中都很好用。和FireFox,但它不適用於Chrome或Safari。在Chrome和Safari中,我都可以看到成功提交的頁面,但我沒有收到發送給我的電子郵件。未在Chrome和Safari中提交表單

HTML頁面:

<form name="balxfrform" action="baltransfer.php" method="POST"> 
<input type="hidden" name="_SUBJECT" value="Transfer Request Form"> 

<b>* Name:</b> <input name="name" type="text" size="60"><br> 
<b>* Email:</b> <input name="email" type="text" size="60"><br> 
<b>Member Number (Last 3 Digits) XXX:</b> <input name="account" type="text" size="10"><br> 
<b>Card Number:</b> <input name="ccnumber" type="text" size="40"><br> 
<b>Phone Number:</b> <input name="pnumber" type="text" size="20"><br> 
<b>Best Time to reach you<sup>1</sup>:</b> <input name="time" type="text" size="40"><br> 
<b>* I agree to the terms and conditions listed below:</b> Yes <input name="terms" type="checkbox" value="Yes"><br>  

    <input type="submit" value="Submit"> 
    </form> 

PHP頁面:

<?php 

if(isset($_POST['email'])) { 
    $email_to = "[email protected]"; 
    $email_subject = "Transfer Request Form"; 

    function died($error) { 
     echo "We are very sorry, but there were error(s) found with the form you submitted.<br /><br /> "; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix the error(s).<br /><br />"; 
     die(); 
    } 

    if(!isset($_POST['name']) || 
     !isset($_POST['email']) || 
     !isset($_POST['account']) || 
     !isset($_POST['ccnumber']) || 
     !isset($_POST['pnumber']) || 
     !isset($_POST['time']) || 
     !isset($_POST['terms'])) { 
     died('We are sorry, but there appears to be a problem with the form you submitted.');  
    } 

    $name = $_POST['name']; // required 
    $email_from = $_POST['email']; // required 
    $account = $_POST['account']; // not required 
    $ccnumber = $_POST['ccnumber']; // not required 
    $pnumber = $_POST['pnumber']; // not required 
    $time = $_POST['time']; // not required 
    $terms = $_POST['terms']; // required 
    $error_message = ""; 

    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
    if(!preg_match($email_exp,$email_from)) { 
     $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 

    $string_exp = "/^[A-Za-z .'-]+$/"; 
    if(!preg_match($string_exp,$name)) { 
     $error_message .= 'The Name you entered does not appear to be valid.<br />'; 
    } 
    if(strlen($error_message) > 0) { 
     died($error_message); 
    } 
    if(!isset($terms)) { 
     $error_message .= 'You must agree to the Terms and Conditions to continue.'; 
    } 
    $email_message = "Form Details Below.\n\n"; 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message .= "Name: ".clean_string($name)."\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\n"; 
    $email_message .= "Member Number XXX: ".clean_string($account)."\n"; 
    $email_message .= "Card Number: ".clean_string($ccnumber)."\n"; 
    $email_message .= "Telephone: ".clean_string($pnumber)."\n"; 
    $email_message .= "Best Time to be Reached: ".clean_string($time)."\n"."\n"; 
    $email_message .= "Agree to Terms and Conditions: ".clean_string($terms)."\n"; 

    // create email headers 
    $headers = 'From: '.$email_from."\r\n". 
     'Reply-To: [email protected]'.$email_from."\r\n" . 
     'X-Mailer: PHP/' . phpversion(); 
    mail($email_to, $email_subject, $email_message, $headers); 
    ?> 
+0

在錯誤日誌中是否有錯誤?大多數情況下,PHP與瀏覽器無關,你知道。 – bemontibeller

+0

@Barmar感謝它的工作。我現在看到的唯一問題是我問了一個有10個測試表單的同事。他獲得了成功完成的頁面,但我沒有收到電子郵件。你知道爲什麼會發生這種情況嗎?再次感謝你的幫助。 – Jaime

+1

什麼工作?我沒有發佈答案。檢查你的PHP錯誤日誌,看看是否有問題。 – Barmar

回答

0

可能會有無關PHP的一部分。 PHP在服務器端運行。由於一個瀏覽器不同地解釋HTML代碼,這可能是HTML表單的HTML語法問題。您可以嘗試驗證您的HTML。試試這個tool

0

請嘗試以下解決方案 1.)在郵件()函數前添加@ 例如:@mail($ email_to,$ email_subject,$ email_message,$ headers);

2.)if (!mail(...)) { // again Call your code }