【问题标题】:The jQuery looks great. Now how do I get the PHP email to send?jQuery 看起来很棒。现在我如何让 PHP 电子邮件发送?
【发布时间】:2010-07-05 17:34:52
【问题描述】:

在提交电子邮件上的提交按钮后,我正在使用 jQuery 替换图像 here。这部分工作得很好,但是,我还需要从表单内容生成电子邮件。

这是表格:

            <form action="estimate.php" action="post">
                <fieldset>
                <input type="text" name="name" value="FULL NAME" onfocus="if (this.value=='FULL NAME') this.value='';"/>
                <input type="text" name="phone" value="PHONE NUMBER" onfocus="if (this.value=='PHONE NUMBER') this.value='';"/>
                <input type="text" name="email" value="EMAIL" onfocus="if (this.value=='EMAIL') this.value='';"/>
                <input type="text" name="date" value="MOVE DATE" onfocus="if (this.value=='MOVE DATE') this.value='';"/>
                <input type="text" name="origin" value="ORIGINATING ADDRESS" onfocus="if (this.value=='ORIGINATING ADDRESS') this.value='';"/>
                <input type="text" name="destination" value="DESTINATION ADDRESS" onfocus="if (this.value=='DESTINATION ADDRESS') this.value='';"/>
                <select name="move-type">
                    <option value="" selected="selected">TYPE OF MOVE</option>
                    <option value="Private">Private</option>
                    <option value="Commercial">Commercial</option>
                </select>
                <input id="quoteSubmit" 
                    type="image" src="_images/btn_submit.png" alt="" 
                    onmouseover="javascript:this.src='_images/btn_submit-over.png'" 
                    onmouseout="javascript:this.src='_images/btn_submit.png'"/>
                </fieldset>
            </form>

这里是 jQuery:

        // free quote form
    $('#freeQuote form').submit(function(e){

    //Set the data ready for ajax post
    var formdata = $(this).serialize();

    $.post("estimate.php",formdata,function(data){
        if(data.error)
        {
           alert('Error: ' + data.error);
           return;
        }
    });

    //The Image
    var Image = $('<img />').attr({src:'_images/free-quote-confirm.png', width:231, height:267, alt:"Success"});

    //Remove the form
    $('#freeQuote form').remove()

    //Add the image inside the div
    $('#freeQuote').append(Image);

    //Return false so the form does not send as normal. you can also use e.PreventDefault():
    return false;
});

这里是 PHP:

<?php # sends contents of  Free Estimate form

if (isset($_POST['submitted'])) {
$errors = array();

// Check for empty fields

if (empty($_POST['name'])) {
    $errors[] = 'Please enter your name.';
} 

if (empty($_POST['email'])) {
    $errors[] = 'Please enter your email address.';
} 

if (empty($_POST['date'])) {
    $errors[] = 'Please enter the date of your move.';
} 

if (empty($_POST['origin'])) {
    $errors[] = 'Please enter the origin of your move.';
} 

if (empty($_POST['destination'])) {
    $errors[] = 'Please enter the destination.';
} 

if (empty($errors)) { // everything is okay

$body = "The following Free Estimate Request has been submitted:\n

    Submitted by: {$_POST['name']}\r
    E-mail: {$_POST['email']}\r
    Phone: {$_POST['phone']}\r
    Move date {$_POST['date']}\r
    Moving from: {$_POST['origin']}\r
    Moving to: {$_POST['destination']}\r
    Move type: {$_POST['move-type']}\r;

    mail ('forrest@rouviere.com', 'Free Estimate Request', $body, 'From: admin@movingsimplified.com');      

    // end email

} else {    
    echo '<h2>Error!</h2>
    <p class="errors">The following error(s) occurred:<br />';
    foreach ($errors as $msg) {
        echo " - $msg<br />\n";
    }
    echo '</p><p>Please go back and try again.</p><p><br /></p>';
}
};

?>

基本问题是,我可以单击提交按钮而无需在字段中输入任何内容,而且我没有收到电子邮件。

任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 您的邮件守护进程配置正确吗?尝试一个精简的邮件脚本。
  • 有点跑题了,至少对于这个问题,重构那个错误代码!!!
  • 我们很感激你们,但是,如果我知道答案,我就不会问这些问题了。我是来学习的!

标签: php jquery forms


【解决方案1】:

除非你的代码片段是正确的,否则你会遇到以下问题..

$body = " // there is no closing "

您还在问题中陈述:

基本问题是,我可以单击提交按钮而无需在字段中输入任何内容,而且我没有收到电子邮件。

您的示例会建议(如果没有语法错误)您不希望在有错误时发送电子邮件。

新更正的 request.php

<?php # sends contents of  Free Estimate form

if (isset($_POST['submitted'])) {
    $errors = array();

    // Check for empty fields

    $checkArray = array('name', 'email', 'date', 'origin', 'destination');
    foreach($checkArray as $check) {
        if (empty($_POST[$check])) {
            $errors[] = 'Please enter your '.$check;
        } 
    }

    if (empty($errors)) { // everything is okay

        $body = "The following Free Estimate Request has been submitted:\n

            Submitted by: {$_POST['name']}\r
            E-mail: {$_POST['email']}\r
            Phone: {$_POST['phone']}\r
            Move date {$_POST['date']}\r
            Moving from: {$_POST['origin']}\r
            Moving to: {$_POST['destination']}\r
            Move type: {$_POST['move-type']}\r;

        ";

        mail ($to, 'Free Estimate Request', $body, 'From:  '.$from);      

        // end email

    } else {    
        // If error then NO Email sent
        echo '<h2>Error!</h2>
        <p class="errors">The following error(s) occurred:<br />';
        foreach ($errors as $msg) {
            echo " - $msg<br />\n";
        }
        echo '</p><p>Please go back and try again.</p><p><br /></p>';
    }
};

?>

【讨论】:

  • 蜥蜴,感谢您对 php 代码的帮助。我把它放在适当的位置并添加了一个 $to 变量,但是,我仍然没有收到电子邮件。没有错误,但也没有电子邮件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
相关资源
最近更新 更多