douyan8266 2017-04-17 14:10
浏览 31
已采纳

联系表格与Php和Ajax发送部分电子邮件

I have designed the following slick Form and using it with PhP & ajax to submit ... its working fine including sending email to the targeted email ID .. There are total Seven (07) fields which suppose to be filled by the visitor. the problem is that when Someone submit the form with all fields I can only receive three (03) fields (Name, Email & Message).... the rest of the Four (04) fields (Phone,country,Budget & Select) are not showing in email... I am sure there must be some problem with my Php. Please tell me where I am mistaking in the following Php.

<?php

// Define some constants
define( "RECIPIENT_NAME", "John Smith" );
define( "RECIPIENT_EMAIL", "example@example.com" );
define( "EMAIL_SUBJECT", "Visitor Message" );

// Read the form values
$success = false;
$senderName = isset( $_POST['senderName'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['senderName'] ) : "";
$senderEmail = isset( $_POST['senderEmail'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderEmail'] ) : "";
$senderPhone = isset( $_POST['senderPhone'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderPhone'] ) : "";
$senderCountry = isset( $_POST['senderCountry'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderCountry'] ) : "";
$senderBudget = isset( $_POST['senderBudget'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderBudget'] ) : "";
$senderSelect = isset( $_POST['senderSelect'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderSelect'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";

// If all values exist, send the email
if ( $senderName && $senderEmail && $senderPhone && $senderCountry && $senderBudget && $senderSelect && $message ) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = "From: " . $senderName . " <" . $senderEmail . ">";
  $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
}

// Return an appropriate response to the browser
if ( isset($_GET["ajax"]) ) {
  echo $success ? "success" : "error";
} else {
?>
<html>
  <head>
    <title>Thanks!</title>
  </head>
  <body>
  <?php if ( $success ) echo "<p>Thanks for sending your message! We'll get back to you shortly.</p>" ?>
  <?php if ( !$success ) echo "<p>There was a problem sending your message. Please try again.</p>" ?>
  <p>Click your browser's Back button to return to the page.</p>
  </body>
</html>
<?php
}
?>
  • 写回答

1条回答 默认 最新

  • duanluanlang8501 2017-04-17 14:21
    关注

    You have not added phone, country, budget and select fields while mailing. Try this:

    if ( $senderName && $senderEmail && $senderPhone && $senderCountry && $senderBudget && $senderSelect && $message ) {
    
        $msgToSend = "Name: ".$senderName."
    ";
        $msgToSend .= "Email: ".$senderEmail."
    ";
        $msgToSend .= "Phone: ".$senderPhone."
    ";
        $msgToSend .= "Sender Country: ".$senderCountry."
    ";
        $msgToSend .= "Sender Budget: ".$senderBudget."
    ";
        $msgToSend .= "Sender Select: ".$senderSelect."
    ";
        $msgToSend .= "Message: ".$message;
    
        $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
        $headers = "From: " . $senderName . " <" . $senderEmail . ">";
        $success = mail( $recipient, EMAIL_SUBJECT, $msgToSend, $headers );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?