...

View Full Version : Creat a unique ID number as the value of a hidden input field



kenneyW00
08-14-2008, 03:17 PM
:confused: 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.

ninnypants
08-14-2008, 03:24 PM
This will do it

document.formname.fieldname.value = UID

Philip M
08-14-2008, 03:55 PM
UID = new Date().getTime().toString();
UID = UID.substr(7,6);

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

kenneyW00
08-14-2008, 03:55 PM
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.

Forever Zero
08-14-2008, 04:42 PM
Have you tried:


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.

Philip M
08-14-2008, 04:44 PM
Try this:-


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

kenneyW00
08-17-2008, 02:26 AM
The function seemed to do the trick. Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum