电子邮件成功发送后如何插入数据

时间:2014-06-06 13:31:19

标签: php ajax email phpmailer

我有问题需要帮助..几乎整天我都这样做而且根本不知道它...我有一个联系表单,带有发送电子邮件和自动回复的功能..但我需要添加更多功能,如能够插入电子邮件成功发送后到数据库..我附上脚本..

联系.html

<form action="<?=$_SERVER['PHP_SELF']?>" method="post" id="contact_form" class="hubungi">
          <input name="idContact" type="hidden" value="" />
          <div id="mail_success" class="success"><img src="success.png" />Thanks..</div>
          <div id="mail_fail" class="error"><img src="error.png" /> Sorry, we don't know what happened. Please try again later.</div>
          <!-- sini-->
          <table width="100%" border="0" cellspacing="2" cellpadding="2">
            <tr>
              <td>Bahagian</td>
              <td><div class="inner-table">
                  <div class="left">
                    <select name="topic" id="topic">
                      <option value="">Please select a topic...</option>
                      <option value="op1">op2</option>
                      <option value="op2">op3</option>
                    </select>
                  </div>
                  <div class="right">
                    <div id="topic_error" class="error"><img src="error.png" /> What category should this be filed in?</div>
                  </div>
                  <div class="clear"></div>
                </div></td>
            </tr>
            <tr>
              <td>Nama</td>
              <td><div class="inner-table">
                  <div class="left">
                    <input class="contact_name" type="text" name="name" id="name" />
                  </div>
                  <div class="right">
                    <div id="name_error" class="error"><img src="error.png" /> What category should this be filed in?</div>
                  </div>
                  <div class="clear"></div>
                </div>
            </tr>
            <tr>
              <td>Email</td>
              <td><div class="inner-table">
                  <div class="left">
                    <input class="contact_email" type="text" name="email" id="email" />
                  </div>
                  <div class="right">
                    <div id="email_error" class="error"><img src="error.png" /> What category should this be filed in?</div>
                  </div>
                  <div class="clear"></div>
                </div></td>
            </tr>
            <tr>
              <td>Subjek</td>
              <td><div class="inner-table">
                  <div class="left">
                    <input class="contact_subject" type="text" name="subject" id="subject" />
                  </div>
                  <div class="right">
                    <div id="subject_error" class="error"><img src="error.png" /> What category should this be filed in?</div>
                  </div>
                  <div class="clear"></div>
                </div></td>
            </tr>
            <tr>
              <td>Mesej</td>
              <td><div class="inner-table">
                  <div class="left">
                    <textarea class="contact_message" name="message" id="message" ></textarea>
                  </div>
                  <div class="right">
                    <div id="message_error" class="error"><img src="error.png" /> What category should this be filed in?</div>
                  </div>
                  <div class="clear"></div>
                </div></td>
            </tr>
          </table>

          <!-- sini tamat-->
          <div id="cf_submit_p">
            <input class="submit" type="submit" id="send_message" value="Send Message">
          </div>
        </form>

的javascript

$(document).ready(function(){
    $('#send_message').click(function(e){
        e.preventDefault();
        var error = false;
        var topic = $('#topic').val();
        var name = $('#name').val();
        var email = $('#email').val();
        var subject = $('#subject').val();
        var message = $('#message').val();
        if(topic.length == 0){
            var error = true;
            $('#topic_error').fadeIn(500);
        } else {
            $('#topic_error').fadeOut(500);
        }
        if(name.length == 0){
            var error = true;
            $('#name_error').fadeIn(500);
        } else {
            $('#name_error').fadeOut(500);
        }
        if(email.length == 0 || email.indexOf('@') == '-1'){
            var error = true;
            $('#email_error').fadeIn(500);
        } else {
            $('#email_error').fadeOut(500);
        }
        if(subject.length == 0){
            var error = true;
            $('#subject_error').fadeIn(500);
        } else {
            $('#subject_error').fadeOut(500);
        }
        if(message.length == 0){
            var error = true;
            $('#message_error').fadeIn(500);
        } else {
            $('#message_error').fadeOut(500);
        } if(error == false){

            $.post("send_email.php", $("#contact_form").serialize(),function(result){
                if(result == 'sent'){
                    $('.hubungi').submit();
                    $('#cf_submit_p').remove();
                    $('table').hide();
                    $('#mail_success').fadeIn(500);
                } else {
                    $('#mail_fail').fadeIn(500);
                    $('#send_message').removeAttr('disabled').attr('value', 'Send Message');
                }
            });
        }
    });
});

仅发送了PHP电子邮件

<?php
$autoResponse = true; //if set to true auto response email will be sent, if you don't want autoresponse set it to false
$autoResponseSubject = "Demo Contact Form"; 
$autoResponseMessage = "Hi, thank you testing the JQuery Contact Form Demo.";
$autoResponseHeaders = "From: email_from@yourWebsite.com";  

//we need to get our variables first
$email_to =   'some@mail.com'; //the address to which the email will be sent
$topic    =   $_POST['topic'];
$name     =   $_POST['name'];
$email    =   $_POST['email'];
$subject  =   $_POST['subject'];
$msg  =   $_POST['message'];

$message = "From: $name \r\nEmail: $email \r\nTopic: $topic \r\nMessage: \r\n$msg";

/*the $header variable is for the additional headers in the mail function,
 we are asigning 2 values, first one is FROM and the second one is REPLY-TO.
 That way when we want to reply the email gmail(or yahoo or hotmail...) will know
 who are we replying to. */
$headers  = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";



if(mail($email_to, $subject, $message, $headers)){
    if($autoResponse === true){
        mail($email, $autoResponseSubject, $autoResponseMessage, $autoResponseHeaders);

    }


    echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..
}else{
    echo 'failed';// ... or this one to tell it that it wasn't sent
}

&GT;

1 个答案:

答案 0 :(得分:0)

if(mail($email_to, $subject, $message, $headers)){
    if($autoResponse === true){
        $success = mail($email, $autoResponseSubject, $autoResponseMessage, $autoResponseHeaders);
        if ($success){
            //update database
           } else {
            //throw error
           }
    }