PHP mail()发送2封电子邮件而不是1

时间:2015-05-20 01:45:37

标签: php email

我有一个我创建的表单,它只是一个带有文本框和提交按钮的简单表单。该文本框用于发送电子邮件的电子邮件地址。

这是我的代码:

//Get First Name Value
    if($is_logged_id && isset($user_data['FirstName'])) $first_name = $user_data['FirstName'];
    //Get Last Name Value
        if($is_logged_id && isset($user_data['LastName'])) $last_name = $user_data['LastName'];?>

    <?php
        //Get Phone Number
        if($is_logged_id && isset($user_data['Phones'][0]->Number)) $phone_number = $user_data['Phones'][0]->Number;?>

    <?php
        //Get Last 4 digits of phone number
        $last_four = substr($phone_number, -4);
        //Add Phone Number to last name
        $ref_code = $last_name . $last_four;
    ?>

    <?php

    //if "email" variable is filled out, send email
    if (isset($_REQUEST['refemail']))  {

        $email_from = $first_name . $last_name . "<" . $user_data['EmailAddress'] . ">";

        // Headers
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= 'From: ' . $email_from . "\r\n";

        //Email information
        $refemail = $_REQUEST['refemail'];
        $subject = $first_name;

        //send email
        mail($refemail, $first_name , $refcode , $headers);


        //Email response
        echo "<p style='padding: 10px;'>Email Response</p>";
    }

    //if "email" variable is not filled out, display the form
    else  {

        ?>

        <form method="post">
            <label for="refemail" class="control-label col-sm-3 col-md-3 col-xs-12 text-left ">Email Address:</label>
            <div class="form-group"></div>
                <input name="refemail" type="text" size="50" class="form-control" /><br />
                <input type="submit" class="ref-btn" value="Submit" />
            </div>

        </form>
    <?php
    }
    ?>

一切都按照我想要的方式工作,除了发送两封完全相同的电子邮件而不是一封。我做了一些搜索,我找不到任何解决我问题的方法。

我尝试在mail()函数下添加以下内容:

exit("sent");

它仍会发送两封电子邮件。

我的代码是否有问题,或者这可能是我的主机的问题?

感谢。

1 个答案:

答案 0 :(得分:1)

我猜表单提交两次,刷新页面或提交表单后再刷新。

此处echo "<p style='padding: 10px;'>Email Response</p>";,如果刷新,此处您只会看到一个“电子邮件回复”。

这是为了防止刷新提交(Jquery):

$('#form').submit(function(e){
    e.preventDefault();
    //then do the submit.
});

或者 你只需要替换按钮类型进行测试

<button type="button">

----------------------------------------------- ----------

Ajax Post数据示例:

form.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

<form method="post" id='form'>
   <label for="refemail" class="control-label col-sm-3 col-md-3 col-xs-12 text-left ">Email Address:</label>
   <input name="refemail" id='refemail' type="text" size="50" class="form-control" /><br />
   <input type="submit" class="ref-btn" value="Submit" />
</form>      

<script>
$(function(){
    $('#form').submit(function(e){
        e.preventDefault();
        data = {
            refemail: $('#refemail').val(),
        };
        $.ajax({
            type: "POST",
            url: './sendemail.php',
            dataType: "html",
            timeout: 50000, 
            data: data,
            success: function(data) {
                alert(data);    
            },
            error: function(request, status, err) {}
        });

    });
});
</script>

sendemail.php

<?php 
if(isset($_REQUEST['refemail'])){
            echo $_REQUEST['refemail'];
            //here do the send email 
        }else{
            echo 'No email';
        }
?>