简体   繁体   中英

Generate message after sending iFrame Email

I have used the following guide to ifugre out how to send emails with file uploading without refreshing the page: http://viralpatel.net/blogs/ajax-style-file-uploading-using-hidden-iframe/ and it works fine, except that I'd like to be able to take a message from the php I use to upload the file and send the email, so that I can display that message to the user, on the page where they submitted the form from.

I have this code currently in my contact.php page:

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>';
}

The only problem is getting that message that I've echoed to show up where I'd like it to go. Any help would be greatly appreciated.

The PHP in the iFrame should post unique sessionID in the database with result. In the meanwhile you can do an Ajax call to check the database if the mail is sent.

So we got 3 files

  • Your form (like index.html)
  • Your Mailer in iframe (like sendMail.php)
  • Your status checker (like getStatus.php)

Here we go..

Your IFRAME Mailer:

<?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));

?>

Your Front-end like 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM