[Javascript] Loop problem with dynamic javascript using ASP

David T. Lovering dlovering at gazos.com
Wed Mar 19 08:21:38 CST 2003


My comment about the onSubmit still stands -- your initial difficulty exists
BECAUSE there is an onSubmit declaration in your <form> declaration.  Remove
it, and do a back-end submit inside the handler as indicated, and those
problems will go away.

The routine I wrote to handle the actual field checks doesn't need to know
what the number of fields are -- it can figure it out from the form itself.
If you start using other kinds of inputs (radio buttons, pull-downs, etc.)
this would have to be beefed up slightly, but no biggie.

Where the initial form comes from is not my problem, and I'll leave it to
your ingenuity how to build the second form.  

With respect to naming the fields: JavaScript and ASP have slightly different
capabilities in this regard.  In general, it is a good idea not to try to use
an array element designator as an input name.  Instead, if you DO want to
automate the name generation, try something like this:

<script language='JavaScript'>
<!--
  var numFields;
  function buildInputs(numFields) {
    document.write("<table name='myTable' cellpadding=3 cellspacing=0>\n");
 
    for (var i=0; i<numFields; i++) {
      document.write("<tr><td align='center'><input name='field_" + i + "' type='text' size=30 value=''>");
      document.write("</tr></td>\n");
    }

    document.write("</table>\n");
  } // buildInputs //
// -->
</script>

Put this script inside your existing form (and modify it as is appropriate), and it will automaticly build the number of inputs you require in a nice tidy table.  Obviously, the format you pick for the names themselves can be altered to suit your whim,
and optional labels can be built inside the same loop as separate <td> ... </td>
prefaces to the inputs themselves.

Hope this helps a bit.

-- Dave Lovering

Alexandre Zglav wrote:
> 
> Thanks again David,
> 
> Well i think you guessed wrong for the onSubmit as it is present in the
> declration of my form since the beginning.
> I am going to try to explain what I am trying to do more precisely and
> give you all more code snippets so that you can really understand.
> 
> First, all my pages are generated dynamically by ASP ( server side
> VBscript)  pages and what I am trying to do here is to combine javascript
> with ASP to generate javascript dynamically. I know its possible and I can
> be pigheaded when it comes to try new things for my web site. I suggest
> you visit this site http://www.15seconds.com/issue/000210.htm before you
> go furher so that i am sure that you fully understand what I am trying to
> do.
> 
> The process of my test page is as follow  :
> 
> First form generation
> A user gets on page jsForm.asp.  This page appears differently wether the
> server finds out  that the visitor has submited any form yet or not. If no
> form has been submited a Form is genarated. It contains only one select
> box with numbers from 1 to 5  and a submit button. This fist form is
> submited to jsForm.asp itself and is not checked by javascript.
> 
> Seccond form generation
> Now once the first form is submited, test1.asp is going to retrieve the
> value of the select box and react differently than before by generating
> another form with a variable number of text fields. This number of text
> field is defined by the number chosen in the select box earlier so if 2
> was selected, there is going to be 2 text fields.
> 
> Now an important part I think, is that on their generation, the text
> fields are given different names : name1 , name2 etc ....
> This client side validation script ( javascript ) is generated with this
> seccond form.
> Finaly on, on the seccond form ( the one that is checked by javascript )
> one submit button is generated.  In the form definition there is an
> onSubmit method that sends the content of the form to the CheckForm
> function that you know.
> 
> Javascript validation
> As you already know the javascript is here to perform a final validation
> of the form before it is submitted to another asp page before database
> insertion. I am using a loop to go through all the text fields ( name1,
> name2 etc ... ) to check if they have been filled. The value of Numfrm is
> a dynamic value that corresponds to the number of textfields. My loop will
> loop ( as its name suggests) through all the texfields generated and check
> that it has been filled and I am pretty sure that the problem is here ...
> but I just cant put my hands on it.
> I think the problem comes from the part  document.form1.name[i].value. In
> fact i think that the name[i] will output something like name[1], name[2]
> etc ... and i'd need it to output name1, name2, name3  etc ....
> 
> So finaly I will give you 2 txt documents.  One is the original asp script
> with comments and the javascript, the seccond is the output of this asp
> script into a browser ( it contains only HTML and Javascript) so that you
> can see where the problem is.
> 
> I hope that I have made my self clear enough for you all to understand
> everything .
> 
> Anyway thanks a lot for your precious help.
> 
> _____________________________________
> IT Projects
> Alexandre Zglav
> Heritage Finance and Trust Company
> 12 cours des bastions
> 1205 Geneva
> Switzerland
> Phone :  ++ 41 22 817 31 68
> ----------------------------------------------------------------
> This document should only be read by those persons to whom it is
> addressed  and  is  not intended to be relied upon by any person
> without  subsequent written confirmation of its contents. If you
> have  received  this  e-mail message in error, please destroy it
> and delete it from your computer.
> Any  form of  reproduction, dissemination, copying,  disclosure,
> modification,  distribution  and/or  publication  of this E-mail
> message is strictly prohibited.
> ----------------------------------------------------------------
> 
> "David T. Lovering" <dlovering at gazos.com>
> Sent by: javascript-admin at LaTech.edu
> 18.03.2003 19:20
> Please respond to javascript
> 
> 
>         To:     javascript at LaTech.edu
>         cc:
>         Subject:        Re: [Javascript] Loop problem
> 
> Ah ha!  I smell an 'onSubmit' problem:
> 
>   You need to modify your original form declaration to include an
> 'onSubmit' equivalence which points
> to your checking routine (which can be substantially shortened, BTW), i.e;
> 
>   <form name='myForm' action='javascript:void(null)'
> onSubmit='checkForm(document.myForm)'>
> 
>   If you combine all the helpful hints together, checkForm can be reduced
> to
> 
>   <script language='JavaScript'>
>   <!--
>     var form1;
>     function checkForm(form1) {
>       if (!form) { return false; }
>       if (form == '') { return false; }                                 //
> throat-scratch protection against bogus form
>       if (!document.forms[form1].elements) { return false; }// cure for
> empty form syndrome
>       var Numfrm = document.forms[form1].elements.length;       // notice
> that since 'form1' is a var I don't use ""
>       for (var i=0; i<Numfrm; i++) {
>         if (document.forms[form1].element[i].length == 0) { return false;
> }
>       }
>       return true;
>     }
>   // -->
>   </script>
> 
>   Notice how 'checkName' got squished into one line of code.  Rule of
> thumb: if you are doing something
>   which takes fewer lines of code to do directly than will be required for
> a function declaration, don't use a
>   function.  This keeps down the burden on the heap assignments, and
> decreases the likelihood of a heap-stack
>   collision -- easily the most common cause of the "blue-screen-of-death"
> (BSOD).  Sometimes I violate this
>   rule if a gazillion descending field elements are nested together in the
> variable name, but I feel guilty
>   about it afterwards.
> 
>   Also, the 'action' declaration I picked can be substituted out for
> anything of your choosing -- you'll
>   probably want to point it to your friendly neighborhood CGI/BIN server
> which handles your other forms
>   submissions.  However, the onSubmit kicks in BEFORE the action gets
> invoked, and if checkForm returns a
>   false, the action is disabled and the form contents don't go anywhere
> (in theory).  Sometimes, a broken
>   FrontPage build of the form will send it on anyway, thereby crashing the
> forms server and endearing you
>   (and Microsoft) to thousands of freshly outraged customers.  [Hence the
> reason I don't use FrontPage].
> 
>   Again, I caution you -- if you have ANYTHING in your form which is an
> element, but not a vanilla text
>   input, this simple script won't cut it anymore, and you'll have to put
> some more hair on its chest.
> 
>   -- Dave Lovering
> 
> 
> Alexandre Zglav wrote:
> >
> > Ok thanks a lot for the tips.
> >
> > I have applied a few changes to my code accordingly to your last email
> and
> > the script  doesn't jam anymore. I still have a problem thought : the
> > tests seem to be completely bypassed. If i try my form and leave it
> blank,
> > the form is submitted and there is no alert ....
> >
> > Oh and by the way you guessed right : i am trying to test variable
> amount
> > of text fields passing them into CheckName
> >
> > Here is the code I am using now :
> >
> >
> >
> > Probably that the return true is missplaced ( its in commentary right
> now
> > as I'm not sure on where to place it )
> >
> > Thanks again for your help.
> >
> >
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> >                             Name: New Text Document.txt
> >    New Text Document.txt    Type: Plain Text (text/plain)
> >                         Encoding: base64
> 
>   ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>                     Name: dlovering.vcf
>    dlovering.vcf    Type: VCard (text/x-vcard)
>                 Encoding: base64
> 
>                  Name: jsform.htm
>    jsform.htm    Type: Hypertext Markup Language (text/html)
>              Encoding: base64
> 
>                  Name: jsForm.asp
>    jsForm.asp    Type: unspecified type (application/octet-stream)
>              Encoding: base64
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dlovering.vcf
Type: text/x-vcard
Size: 304 bytes
Desc: Card for David T. Lovering
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20030319/ebaac268/attachment.vcf>


More information about the Javascript mailing list