Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-28-2012, 04:56 AM   PM User | #1
VonDeLaus
New to the CF scene

 
Join Date: Jun 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
VonDeLaus is an unknown quantity at this point
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..
VonDeLaus is offline   Reply With Quote
Old 06-28-2012, 07:46 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,098
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
VonDeLaus (06-28-2012)
Old 06-28-2012, 08:31 AM   PM User | #3
VonDeLaus
New to the CF scene

 
Join Date: Jun 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
VonDeLaus is an unknown quantity at this point
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.
VonDeLaus is offline   Reply With Quote
Old 06-28-2012, 08:36 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,098
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
VonDeLaus (06-28-2012)
Old 06-28-2012, 09:18 AM   PM User | #5
VonDeLaus
New to the CF scene

 
Join Date: Jun 2012
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
VonDeLaus is an unknown quantity at this point
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.
VonDeLaus is offline   Reply With Quote
Reply

Bookmarks

Tags
form, integer, random, random integer

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:00 AM.


Advertisement
Log in to turn off these ads.