Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Random Integer upon Form Submission

    Hey guys, I'm new here, and after some searching, I haven't been able to find an answer to my problem. I'm totally new to HTML and JavaScript, so I'm learning as I go, and have usually just been able to google something and use code I find that way, but I can't seem to teach myself how to get a random number generated upon a form being submitted.

    Here's exactly what I'm trying to do: I have a form. There are two text fields that someone can enter information into. Once they hit submit (And not before) I want it to generate a random number between 1 and 100 to be included in the e-mail notification.

    If you guys need any more information, please don't hesitate to ask, and I'd like to thank you all in advanced for any assistance you can offer me.
    Last edited by VonDeLaus; 06-28-2012 at 09:56 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    This will generate a random number 1-100 and place it in a hidden field:-



    Code:
    <form action = "";>
    <input type = "hidden" name = "rnum">
    <br><br>
    <input type = "button" value = "Submit" onclick = "randnum()">
    </form>
    <script type = "text/javascript">
    
    function randnum() {
    var randy = 1 + Math.floor(100* Math.random() );
    document.forms[0].rnum.value = randy;
    document.forms[0].submit();
    }
    
    </script>
    You refer to "email". I hope you are not trying to use mailto: which is obsolete. You need to submit your form to a server-side script.

    Quizmaster: The Italian company Gaggia manufactures machines for making what drink?
    Contestant: Whisky.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    VonDeLaus (06-28-2012)

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Honestly, I feel a little in over my head. lol

    Thank you so much for your reply! It's not currently working, it seems, but that may be how I'm getting the e-mails sent to me. Currently, I'm using a free form creator from the web, and it sends me e-mail submissions of the form, but I think that may be creating the issue. I'm researching how to create your own form with e-mail submission using PHP or something, so I'll look into it more. If you have any information on that, it'd be greatly appreciated, but since this top is about the random integer, I'll understand if you don't care to address that issue here.

    I figured it'd be easy, since all I wanted was two text fields and the hidden integer, but it's giving me more issues than the much larger application form I made!

    Thanks again for your reply.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    For a simple PHP form feedback script see:-

    http://www.thesitewizard.com/archive/feedbackphp.shtml

    or http://www.felgall.com/php2.htm

    Can I ask what is the point of the random integer with such a small range 1-100?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    VonDeLaus (06-28-2012)

  • #5
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I got it working! Huzzah! Thank you so much for your assistance! You've been a great help, and very prompt.

    This form is for a role-playing site that's based on D&D, and I'm making a form for users to submit a log of them crafting something, and their success is based on a roll of a 100 sided die. If it is equal to or lower than their 'skill number', it is successful. Which is why I wanted it hidden until submission. If it was visibile BEFORE submission, people could cheat the system.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •