0

I need a button to call a php page though ajax. I need the button to open the client's email with a mailto link. The php page generates the email, which consist of an encrypted string that passes credentials to a secured site.

Basically, I need these things to happen in this order:

Click the button. Make the ajax call. Populate href with "mailto:subject=your_secret_link&body=http://securesite.com?authcode=encrytpedstuff" Open the client's email.

I've tried this: html part:

<span id="mailframe"><a href=# id="myemail"><input name="Request Signing via Email"  value="Request Signing via Email" type="button" class="redButton" onclick="sendEmail();"/></a></span>

javascript part

function sendEmail() {
    var hash=document.getElementById('hash').value;
    var obj=document.getElementById('mailframe');
    var email=document.getElementById('myemail');
    var mailxml = new XMLHttpRequest;

    mailxml.onreadystatechange = function() {

        if ((mailxml.readyState == 4) && ((mailxml.status == 302)|| (mailxml.status == 200))) {
            email.href=mailxml.responseText;
        }
    }

    mailxml.open("GET",'/secure/literature/generateid.php?hash='+hash+'&docid=<?=$docid?>' );
    mailxml.send();
}

It works in IE, but not in Firefox. Any ideas?

1 Answers1

0

I think that you need something like this (works with FireFox 24):

    <script type="text/javascript">
        function sendEmail() {
            var hash=document.getElementById('hash').value;

            var email=document.getElementById('myemail');
            var mailxml = new XMLHttpRequest;

            mailxml.onreadystatechange = function() {
                if (mailxml.readyState == 4 && ((mailxml.status == 302) || (mailxml.status == 200))) {
                    window.location.href = mailxml.responseText;
                }
            };

            mailxml.open("GET", "/your/long/url?with=parameters", true);
            mailxml.send();}
    </script>

    <div>
        <input type="button" onclick="sendEmail()" value="Send E-mail"/>
        <input type="text" name="hash" id="hash" value="hashValue" />
        <a href="#" id="myemail">E-mail link</a>
    </div>

Also check Easiest way to retrieve cross-browser XmlHttpRequest and braces in code.

Community
  • 1
  • 1
CrazyMORF
  • 268
  • 2
  • 8
  • Thanks, but unfortunately I must keep all of this under a single button. I can't have a separate link. So the button has to go through the whole process in the proper steps. – user186445 Sep 23 '13 at 18:33
  • I can't answer my own question, so I'll post my finding here: I just do a window.location=email.href within the mailxml function. Code is genned by the generateid.php page in a mailto: link, then the page immediately opens up the link. – user186445 Sep 23 '13 at 18:55