...

View Full Version : prefix form values



Jordan656
04-02-2009, 05:51 PM
hi.

I have this nifty little script I am using to generate a unique reference number for everyone that fills in my form. I have the same form in 2 locations, and it is important that I can tell (from the reference number alone) which location it came from.

I know all about sending the url etc from a form in its own hidden field, but for 100 very boring reasons I need to be able to tell from the number. I have been trying to prefix the number with letters, so from one location the number would be AAxxxxxxxx, and from the other location it would be BBxxxxxx but I have been unable to do so.

Any suggestions? Form script below (has had all irrelevant fields taken out to simplify)


<form id="formname" name="formname" method="post" action="./testcontrol.php">
<input type="text" name="orderNum" id="orderNum" />
<SCRIPT type="text/javascript">
var d = new Date();
var yr =( d.getYear() ).toString();
yr = yr.charAt(3); // final digit
var mo = ( d.getMonth() ). toString();
if ( mo.length <2 ) {mo = 0 + mo}
var hrs = ( d.getHours() ). toString();
if (hrs.length <2) {hrs = 0 + hrs}
var mns = ( d.getMinutes() ).toString();
if (mns.length <2) {mns = 0 + mns}
var secs = (d.getSeconds() ).toString();
if (secs.length <2) {secs = 0 + secs}
var oNum = yr + mo + hrs + mns + secs;
document.formname.orderNum.value = oNum;
</SCRIPT>
</form>

VIPStephan
04-02-2009, 06:44 PM
How about removing the name attribute from the form (as that’s outdated, deprecated, and invalid), wrapping all form controls in a block-level element (since that’s the only valid way), and doing something like this:


<form id="form1" method="post" action="./testcontrol.php">
<div>
<input type="text" name="orderNum" id="orderNum" />
</div>
<script type="text/javascript">
var d = new Date();
var yr =( d.getYear() ).toString();
yr = yr.charAt(3); // final digit
var mo = ( d.getMonth() ). toString();
if ( mo.length <2 ) {mo = 0 + mo}
var hrs = ( d.getHours() ). toString();
if (hrs.length <2) {hrs = 0 + hrs}
var mns = ( d.getMinutes() ).toString();
if (mns.length <2) {mns = 0 + mns}
var secs = (d.getSeconds() ).toString();
if (secs.length <2) {secs = 0 + secs}
var oNum = yr + mo + hrs + mns + secs;

var formID = document.forms['form1'].getAttribute('id');
document.forms['form1'].orderNum.value = formID + oNum;
</script>
</form>

(I haven’t tested it.)

Oh and by the way: Maybe this can be of help: http://javascript-coder.com/javascript-form/javascript-forms-get-all-form-objects.htm



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum