Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creat a unique ID number as the value of a hidden input field

    I must be having a serious brain cramp. I want to create a unique ID number and then insert it into a hidden field value for each person who fills out my form.
    I am using the code--
    myDate=new Date();
    UID=myDate.valueOf();
    ...to create the unique ID number, but I can’t figure out how to insert it as the value of the hidden field in my form.

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    This will do it
    Code:
    document.formname.fieldname.value = UID

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,530 Times in 2,508 Posts
    UID = new Date().getTime().toString();
    UID = UID.substr(7,6);

    yields a six-digit number and should avoid any possible duplication.

  • #4
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, but I tried that too. It iether sends the whole code as a text string or it doesn't send anything at all. I have tried it inside the hidden input field and outside as a seperate line, with <script> tags and without.

  • #5
    New Coder
    Join Date
    Apr 2005
    Location
    Massachusetts, US
    Posts
    37
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Have you tried:
    Code:
    document.getElementById("yourHiddenFieldID").value = UID;
    I had to do this same thing before and it worked fine, but it looks like this is pretty much what ninnypants told you to do. Could you maybe elaborate more on what exactly happens when you try that, or post full code?

    Calling an alert on the value of the hidden field might help with finding out what's wrong.
    Last edited by Forever Zero; 08-14-2008 at 04:44 PM.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,530 Times in 2,508 Posts
    Try this:-

    Code:
    <input type = "hidden" id = "IDnumber">
    <input type = "submit" value = "Submit" onclick = "addIDnum()">
    
    <script type = "text/javascript">
    
    function addIDnum () {
    
    UID = new Date().getTime().toString();
    UID = UID.substr(7,6);    //  last 6 digits
    document.getElementById("IDnumber").value = UID;
    
    alert ("Unique ID Number = " + document.getElementById("IDnumber").value);  // remove after testing
    
    }
    </script>

  • #7
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    whoot whoot to Philip M...

    The function seemed to do the trick. Thanks


  •  

    Posting Permissions

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