Random Number in hidden form field

Hello,

I was wondering if anyone knows if it is possible to place a randomly generated number inside of a hidden form field. I need to generate a unique “Order Number” on pageload and have the number written to a hidden field in the order form.

Thank You

Just put this in your form (this gets 2 random one-digit numbers and sticks them onto the current timestamp, giving you a pretty good unique number):


<input type="hidden" name="orderNum">
<script>
now = new Date();
randomNum = '';
randomNum += Math.round(Math.random()*9);
randomNum += Math.round(Math.random()*9);
randomNum += now.getTime();
document.formName.orderNum.value = randomNum
</script>

Thank you for your fast reply but I cant get it to work. it says it is undefined. Maybe if i post the code you could be more help. I should have done that in the first place.

<html>
<head>
<title>XYZ Host - checkout page</title>
<script>
function doform()
{
var info;
info = "Domain: " + document.forms[0].domain.value + "
";
info += "Username: " + document.forms[0].username.value + "
";
info += "Password: " + document.forms[0].pwd.value;
document.forms[0].info.value = info;

document.forms[0].submit();
}
</script>
</head>
<body>
<form method=“post” action=“”>

<input type=“hidden” name=“session” value=“xyzhosting”>
<input type=“hidden” name=“cur” value=“GBP”>

You are buying our yearly hosting plan.
<input type=“hidden” name=“product” value=“Yearly hosting plan, 200MB disk space, 25GB bandwidth per month and unlimited email accounts.”><br>

Cost: GBP 150/Year
<input type=“hidden” name=“amt” value=“15000”><br>

Product code: XYZ-Yearly-Hosting-1
<input type=“hidden” name=“pcode” value=“XYZ-Yearly-Hosting-1”><br>

Order number: [B][GENERATED NUMBER HERE][/B]
<input type=“hidden” name=“orderid” value=“[AND HERE]”><br>

Domain name: <input type=“text” name=“domain”><br>

Username: <input type=“text” name=“username”><br>

Password: <input type=“text” name=“pwd”><br>

Comments: <textarea name=“comments” rows=10 cols=10></textarea><p>

<input type=“hidden” name=“info” value=“”>
<input type=“submit” onclick=“doform()”>

</form>
</body>
</html>

If possible I would like the generated number to post in the hidden fiels as well as after the ORDER NUMBER:

Thank You Again

What you could do is put the script in the head, and use the randomNum variable created anywhere you want.

<html>
<head>
<script>
now = new Date();
randomNum = ‘’;
randomNum += Math.round(Math.random()*9);
randomNum += Math.round(Math.random()*9);
randomNum += now.getTime();
</script>
</head>

In Form:

Order Number = <script>document.write(randomNum)</script>
<input type=“hidden” name=“orderid” onload=“this.value=randomNum”><br>

Works Excellent. THANK YOU very much