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 );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错