View Full Version : Resolved Random Integer upon Form Submission

06-28-2012, 05:56 AM
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.

Philip M
06-28-2012, 08:46 AM
This will generate a random number 1-100 and place it in a hidden field:-

<form action = "";>
<input type = "hidden" name = "rnum">
<input type = "button" value = "Submit" onclick = "randnum()">
<script type = "text/javascript">

function randnum() {
var randy = 1 + Math.floor(100* Math.random() );
document.forms[0].rnum.value = randy;


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.

06-28-2012, 09:31 AM
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.

Philip M
06-28-2012, 09:36 AM
For a simple PHP form feedback script see:-


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?

06-28-2012, 10:18 AM
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.