SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Dec 2001
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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):

    Code:
    <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>

  3. #3
    SitePoint Member
    Join Date
    Dec 2001
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Jon

    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 + "\n";
    info += "Username: " + document.forms[0].username.value + "\n";
    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: [GENERATED NUMBER HERE]
    <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

  4. #4
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>

  5. #5
    SitePoint Member
    Join Date
    Dec 2001
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Jon

    Works Excellent. THANK YOU very much


Bookmarks

Posting Permissions

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