发送iFrame电子邮件后生成消息

时间:2013-12-27 15:26:35

标签: javascript php jquery ajax iframe

我已经使用以下指南来了解如何发送带有文件上传的电子邮件而不刷新页面:http://viralpatel.net/blogs/ajax-style-file-uploading-using-hidden-iframe/并且它工作正常,除了我希望能够从php获取消息我用来上传文件并发送电子邮件,以便我可以在他们提交表单的页面上向用户显示该消息。

我目前在contact.php页面中有这个代码:

if (!$sentMail) {
    header('HTTP/1.1 500 Couldnot send mail! Sorry..');
    exit();
} else {
    echo '<h3>Hi ' . $postName . ', Thank you for your email</h3>
        <p>Your email has already arrived in our Inbox, all We need to do is Check it.
        <br />Good day.</p>';
}

唯一的问题是获得我所回应的信息,以显示我希望它去的地方。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

iFrame中的PHP应该在结果中在数据库中发布唯一的sessionID。 同时,如果邮件已发送,您可以执行Ajax调用来检查数据库。

所以我们得到了3个文件

  • 您的表单(如index.html)
  • iframe中的邮件程序(如sendMail.php)
  • 您的状态检查器(如getStatus.php)

我们走了..

您的IFRAME梅勒:

<?php
session_start();

$_SESSION['mailsender'] = mt_rand();

    // this is ur iframe mailer 
    // here your mail send stuff .....


    // if mail is sent
    mysql_query("INSERT INTO mailsender (mailid, result) VALUES ('".$_SESSION['mailsender']."', 'successfull')");

    // if mail fails
    mysql_query("INSERT INTO mailsender (mailid, result) VALUES ('".$_SESSION['mailsender']."', 'failed')");

?>

getStatus.PHP:

<?php
session_start();

// check status and give JSON back
// getStatus.php - we be called from front-end

    $query = mysql_query("SELECT * FROM mailsender WHERE mailid = '".$_SESSION['mailsender']."'");

    $result = "Pending";

    if (mysql_num_rows($query) > 0) {

                while ($row = mysql_fetch_array($query)) {

                    $result = $rij['result'];

                }

    }

    echo json_encode(array("result"=>$result));

?>

你的前端像Index.html:

<!DOCTYPE html>
<html>

    <!-- include jQuery -->

    <script>
    $(document).ready(function(){




    checkMailStatus = function() {

        $.ajax({

        url: 'getStatus.php',
        dataType: 'JSON',

                success: function(data) {

                            if (data['result'] == "successfull") {

                                // do successfull stuff here
                                // also clear the interval

                            }
                            if (data['result'] == "failed") {

                                // do failed stuff here
                            }
                            if (data['result'] == "pending") {

                                // still trying to send
                                // do stuff here while sending (like loading.gif)
                            }


                }



        })

    }

            $(".sendmailbutton").click(function(){

                setInterval(function(){

                    checkMailStatus();

                }, 800)

            })



    })

    </script>

</html>