-1

Right now I'm trying to have a random html output be emailed to me whenever the website page user clicks on the button. They receive a code displayed below the button when clicked. I need to have that random code whenever generated be e-mailed to my address. Here is my present code:

    <body>
    <h1>RandomCode</h1>

    <button onclick="document.getElementById('demo').innerHTML = makeid(1,1000)">Click Me</button>

    <p id="demo"></p>

    <script>
    function makeid()
    {
        var text = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";


        for( var i=0; i < 5; i++ )
            text += possible.charAt(Math.floor(Math.random() * possible.length));

        return text;
    }
    </script>
    </body>

As you can see the random code is outputted into the p tag. I need to have it so that they see the code, but the server automatically e-mails the code without them having to do it. They should be able to generate many codes sending me multiple e-mails. Can anyone help?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
lectrician1
  • 77
  • 1
  • 13
  • 2
    Use AJAX. When use clicks on the button, make an AJAX call to a server side script that receives the random data generated and sends you an email. Might want to look at other PHP libraries that deals with mail sending instead of relying on `mail()`. – Terry Feb 08 '17 at 17:17
  • use mail() http://www.w3schools.com/php/func_mail_mail.asp – ii7scw Feb 08 '17 at 17:18

1 Answers1

0

Use an AJAX request to a PHP page, with the random code:

$.ajax({
      url: 'email.php',
      type: 'post',
      data: {'randomString': 'stringHere'},
      success: function() {
        //do something on success 
      },
      error: function() {
        //do something on fail 
      }
    });

See this question for more help: jQuery Ajax POST example with PHP

Community
  • 1
  • 1
kzhao14
  • 2,470
  • 14
  • 21