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 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2008
    Location
    UK
    Posts
    53
    Thanks
    12
    Thanked 0 Times in 0 Posts

    prefix form values

    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)

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

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,867
    Thanks
    6
    Thanked 1,029 Times in 1,002 Posts
    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:
    Code:
    <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.)

    Edit: Oh and by the way: Maybe this can be of help: http://javascript-coder.com/javascri...rm-objects.htm
    Last edited by VIPStephan; 04-02-2009 at 07:51 PM.

  • The Following 2 Users Say Thank You to VIPStephan For This Useful Post:

    Jordan656 (04-02-2009), oesxyl (04-03-2009)


  •  

    Posting Permissions

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