Ajax表单并不总是发送到电子邮件

时间:2017-12-19 16:31:20

标签: javascript php jquery ajax

我有问题。我有一个表格,我需要通过电子邮件发送到godaddy outlook邮件帐户。问题是表单有时提交,有时则提交。这里是进行ajax调用的js的代码,按钮所在的html(我把它作为一个p,#SendPlan作为id)和发送电子邮件的php(我也用过phpmailer): ($ Type总是正确的,因此它总是在ajax中,所有其他变量都假定已经有值)

$(document).ready(function () {

    $("#SendPlan").click(function () {

        if($Type=="Cake Customization")

        { $.ajax({
            url: "plan_mail.php",
            data: {
                'Type' : $Type,
                'CakeEvent': $CakeEvent,
                'CakeSize': $CakeSize,
                'Date' : $Date,
                'Name' : $Name,
                'Location' : $Location,
                'Email' : $Email,
                'Phone' : $Phone,
                'Notes' : $Notes,
            },

            cache: "false",
            type: "POST",
            success: function(response) {

                document.getElementById('mailStatus').innerHTML="<h2 class=\"planthankyou\">Thank You For Your Submission. We Will Contact You Soon!</h2>";
            },

            error: function (response){
                alert(response);
                $('#dialog-form').dialog('close');}
        });
        }
    });
});

这是ajax正在调用的plan_mail.php:

$Type= $_POST['Type'];
if($Type=="Cake Customization")
{
    $Occasion= $_POST['Occasion']   ;
    $CakeEvent = $_POST['CakeEvent'];
    $CakeSize= $_POST['CakeSize'];
    $Date= $_POST['Date']  ;
    $Name= $_POST['Name']  ;
    $Location= $_POST['Location']  ;
    $Email= $_POST['Email'] ;
    $Phone= $_POST['Phone'] ;
    $Notes= $_POST['Notes'] ;

    $message = "\nDate : " .  $Date ."\nName : " .  $Name . "\nLocation : " .  $Location .   "\nEmail : " .  $Email
        . "\nPhone : " .  $Phone ."\nNotes : " .  $Notes
        ."\nType : " . $Type . "\nCake Event : " . $CakeEvent . "\nCake Size : " .  $CakeSize;
}

require("/home/specialeventsleb/public_html/phpmailer/PHPMailer/class.phpmailer.php");

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port       = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth   = true;
$mail->Username = 'anemail';
$mail->Password = 'apassword';
$mail->SetFrom('anemail', 'FromEmail');
$mail->addAddress('anemail', 'ToEmail');
$mail->Subject = 'Message from Special Events Website';
$mail->Body    = $message;
$mail->send();

最后这是#SendPlan所用的html的一部分:

<div id="Plan">
    <div id="SubPlan">
        <!--Shared Final Section-->
        <div id="FinalStep">
            <div >
                <div class="PlanWrapper" id= "mailStatus" >
                    <div class="row"><div class="col-xtra-12"><h1>CUSTOMIZE YOUR DISPLAY TABLE</h1></div></div>
                    <div class="row"><div class="col-xtra-12" ><p>FINAL STEP</p></div></div>
                    <div class="row">
                        <div class="col-sm-6 col-sm-6">
                            <input name="DecorationOther" placeholder="WHAT IS THE DATE?" class="Date" />
                            <input name="DecorationOther" placeholder="NAME" class="Name"/>
                            <input name="DecorationOther" placeholder="LOCATION" class="Location"/>
                            <input name="DecorationOther" placeholder="E-MAIL" class="Email"/>
                            <input name="DecorationOther" placeholder="TELEPHONE" class="Phone"/> </div>
                        <div class="col-sm-6 col-sm-6"> <textarea name="DecorationOther"  placeholder="NOTES AND MESSAGES" class="Notes"></textarea>  </div>
                    </div>
                    <div class="row"><div class="col-xtra-12" style="text-align:right"><p id="SendPlan">SEND</p></div></div>
                </div>
            </div>
        </div>
    </div>
</div>

0 个答案:

没有答案