简体   繁体   中英

How can I email an html random output using <button>?

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?

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

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