2013-07-29 105 views
0

我有這個腳本是爲了發送電子郵件給我,但我想要做的是,而不是將用戶重定向到不同的頁面來顯示結果,我希望它顯示成功消息或失敗消息與窗體位於同一頁面上。我希望我能讓自己清楚。我正在嘗試將php和html添加到一個頁面。 的index.html:重定向PHP

<!DOCTYPE html> 

<html> 
<head> 
    <title></title> 
</head> 

<body> 
    <form action="send_form_email.php" id="contactform" method="post" name= 
    "contactform"> 
      <table width="450px"> 
        <tr> 
          <td valign="top"><label for="title">Title 
          *</label></td> 

          <td valign="top"><input maxlength="50" name= 
          "title" size="30" type="text"></td> 
        </tr> 

        <tr> 
          <td></td> 
        </tr> 

        <tr> 
          <td valign="top"><label for="fname">First Name 
          *</label></td> 

          <td valign="top"><input maxlength="50" name= 
          "fname" size="30" type="text"></td> 
        </tr> 

        <tr> 
          <td valign="top"><label for="mname">Middle 
          Name</label></td> 

          <td valign="top"><input maxlength="50" name= 
          "mname" size="30" type="text"></td> 
        </tr> 

        <tr> 
          <td valign="top"><label for="lname">Last Name 
          *</label></td> 

          <td valign="top"><input maxlength="50" name= 
          "lname" size="30" type="text"></td> 
        </tr> 

        <tr> 
          <td valign="top"><label for= 
          "suffix">Suffix</label></td> 

          <td valign="top"><input maxlength="80" name= 
          "suffix" size="30" type="text"></td> 
        </tr> 

        <tr> 
          <td valign="top"><label for="message">Message 
          *</label></td> 



     <td valign="top"> 
           <textarea cols="25" maxlength="1000" name= 
           "message" rows="6"> 
</textarea></td> 
         </tr> 
         <tr> 
           <td valign="top"><label for="email">Email 
           Address*</label></td> 

           <td valign="top"><input maxlength="80" name= 
           "email" size="30" type="text"></td> 
         </tr> 

         <tr> 
           <td colspan="2" style="text-align:center"> 
           <input type="submit" value="Submit"></td> 
         </tr> 


      </table> 
    </form> 
</body> 
</html> 

send_form_email.php:

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

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to  = "[email protected]"; 
    $email_subject = "Your email subject line"; 


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

    // validation expected data exists 
    if (!isset($_POST['title']) || !isset($_POST['fname']) || !isset($_POST['mname']) || !isset($_POST['lname']) || !isset($_POST['suffix']) || !isset($_POST['message']) || !isset($_POST['email'])) { 
      died('We are sorry, but there appears to be a problem with the form you submitted.'); 
    } 

    $title_name = $_POST['title']; // required 
    $first_name = $_POST['fname']; // required 
    $middle_name = $_POST['mname']; 
    $last_name = $_POST['lname']; // required 
    $suffix_name = $_POST['suffix']; 
    $message  = $_POST['message']; // required 
    $email_from = $_POST['email']; // required 

    $error_message = ""; 
    $string_exp = "/^[A-Za-z .'-]+$/"; 
    if (!preg_match($string_exp, $title_name)) { 
      $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
    } 
    if (!preg_match($string_exp, $first_name)) { 
      $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
    } 
    if (!preg_match($string_exp, $middle_name)) { 
      $error_message .= 'The Middle Name you entered does not appear to be valid.<br />'; 
    } 
    if (!preg_match($string_exp, $last_name)) { 
      $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; 
    } 
    if (!preg_match($string_exp, $suffix_name)) { 
      $error_message .= 'The Suffix Name you entered does not appear to be valid.<br />'; 
    } 
    if (strlen($message) < 2) { 
      $error_message .= 'The Message you entered do not appear to be valid.<br />'; 
    } 
    if (strlen($error_message) > 0) { 
      died($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 />'; 
    } 
    $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 .= "Title Name: " . clean_string($title_name) . "\n"; 
    $email_message .= "First Name: " . clean_string($first_name) . "\n"; 
    $email_message .= "Middle Name: " . clean_string($middle_name) . "\n"; 
    $email_message .= "Last Name: " . clean_string($last_name) . "\n"; 
    $email_message .= "Last Name: " . clean_string($suffix_name) . "\n"; 
    $email_message .= "Comments: " . clean_string($message) . "\n"; 
    $email_message .= "Email: " . clean_string($email_from) . "\n"; 


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


<!-- include your own success html here --> 

<center>Thank you for contacting us. We will be in touch with you very soon.</center> 

<?php 
} 
?> 

我知道它應該是這個樣子,但我不知道如何使用,在我的代碼:

// validation code 
// in some div 
if($success) { 
    // thanks for contacting us 
} else { 
    // you suck at filling out forms 
} 
// form goes here 
+3

在同一個頁面上顯示成功/錯誤消息的HTML,您必須使用AJAX爲表單或添加PHP代碼來處理同一個文件的形式爲您的HTML – kabuto178

回答

0

在PHP中:

$mail = mail($email_to, $email_subject, $email_message, $headers); 
if($mail){ 
    // success message 
    $msg = "Success - Send Successfully"; 
    echo "<script>top.location.href = 'index.html#$msg';</script>"; 
} else { 
    // Error message 
    $msg = "Error - Email not Send"; 
    echo "<script>top.location.href = 'index.html#$msg';</script>"; 
} 

使用一些JavaScript從url獲得結果到你的index.html文件中,在上面的示例中,我使用散列發送消息,現在使用javascript確定來自url的散列值。

if(window.location.hash) { 
    // Fragment exists 
    //Puts hash in variable, and removes the # character 
    var hash = window.location.hash.substring(1); 

// Determine the success or failure 
var patt=/Success/g; 
var result=patt.test(hash); 

if(result){ 
    // Success Message 
    alert(hash); 
} else { 
    // Error Message 
    alert(hash); 
} 

} 
+0

得益於它完美! –

+0

謝謝! :) 這太棒了! –