Results 1 to 4 of 4

Thread: Create a random number in the value= attribute on a hidden input field

  1. #1
    Join Date
    Aug 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create a random number in the value= attribute on a hidden input field

    I'm wanting to have a randomly generated number as the value of a tag each time I reload the page.

    <form action="whatever.asp" method="post" name="myform" onsubmit="return ValidateForm(this)">

    <input type="hidden" name="v46" value="8773">

    </form>



    I want to replace the 8773 of the attribute value="8873 with a randomly generated number between 1-9999. So far I have found the code below which generates a random number as the value in a visible input field.

    What I need it to do is change in the actual code of the page each time the page reloads for a hidden input value. So evertime you refresh the page the value="8873" in the actual code changes to a different number.

    The JavaScript I have so far is


    Code:
    <script type="text/javascript"> 
    var n=9999; 
    onload=function(){ 
    var rand = Math.ceil(Math.random()*n); 
    document.forms[0].elements['v46'].value=rand 
    } 
    </script>

    Any Ideas? Any help would be greatly appreciated.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The code you have will change the value as you ask, providing that element is in the first form of the page. There's only one thing that puzzles me:
    change in the actual code of the page
    This is not possible without server-side scripting, but more pertinently... why?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The code I am using is on a search page, each page that loads needs to have a different value="" or if two people used the same page at the same time it could cause a mix up in the results returned.

    Would it make any difference between using a hidden vs a non hidden input field?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No.

    The best way to do this is still server-side. Server-side you don't need to generate a random number, since you can use numbers incrementally. This also removes the risk of user tampering.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •