40 users online (0 members and 40 guests)  


  Results 1 to 3 of 3

Related

  1. need help with onclick link to external .JS file    Forum: Javascript Forum
    Replies: 0
  2. Adding values to an external .js Javascript file?    Forum: Javascript Forum
    Replies: 3
  3. [SPLIT]passing values to an external .js file    Forum: Javascript Forum
    Replies: 37
  4. Replies: 1
  5. passing values to an external .js file    Forum: Javascript Forum
    Replies: 10
  1. #1
    woodknut's Avatar
    Disabled

    Status
    Offline
    Join Date
    Dec 2003
    Posts
    32

    help checking form elements in external js file

    I am gathering all user input from a form and then submitting the form data to a shopping cart. Due to the expectations of the shopping cart I am forced to use a variable with brackets as follows:

    <INPUT type="HIDDEN" name="product1[]" value="newProduct">

    Further, I have several INPUT statements like this to gather multiple "descriptions" for a single product item. In the shopping cart the submitted product appears with each "description" delimited by commas, with a single price, quantity, etc (gathered from other INPUTs). Its works but...

    Here's my problem:
    Now, on submit I am validating some of the fields in an external *.js file and am having troubles checking the values in the product1[] item (not sure if its really an array). I am passing the document.myFormName and can access some of the other elements of the form by element name, but the product1[] is giving me fits.

    I'm still new at this and looking forward to your help!
    WoodK.

  2. #2
    cgiexpo's Avatar
    New User

    Status
    Offline
    Join Date
    Dec 2003
    Location
    Washington
    Posts
    6
    Can you provide your JS code and form?

  3. #3
    woodknut's Avatar
    Disabled

    Status
    Offline
    Join Date
    Dec 2003
    Posts
    32

    Code snippets

    I've actually made it work. Though I would appreciate any constructive criticism you may have:

    Placed in <head> section:
    <SCRIPT LANGUAGE="JavaScript1.2" src="tools.js"></script>

    Beginning of form section:

    <FORM name="myForm" method="POST" action="URLtoShoppingCart.cfm">
    <INPUT type="HIDDEN" name="userid" value="xxxxxx">
    <INPUT type="HIDDEN" name="return" value="myCompanyReturnPage.html">
    <INPUT type="HIDDEN" name="product1[]" value="Domain Sign Up">
    <INPUT type="HIDDEN" name="product1[]" value="Agreed to Terms">
    <INPUT type="HIDDEN" name="price1" value="25.99"> <INPUT TYPE="hidden" NAME="qty1" value="1" CLASS="input">

    ...
    ...
    End of form:

    <INPUT type="submit" value=" Submit " onClick="return checkForm( document.myForm )">
    <INPUT TYPE="Reset" NAME="reset" VALUE="Cancel">


    Here's the relevant portions of the javascript file (tools.js):

    function checkForm( thisForm ) {
    if (checkCheckBox( thisForm.agree ) && checkForReferred( thisForm ))
    {
    return true;
    }
    return false;
    }

    function checkForReferred( thisForm ){
    var start = 10 // first referred company field in array
    var opt40 = 5 // elements to check for at 40%
    var opt100 = 4 // elements to check for at 100%
    var vtocheck = 40
    option1Good = checkOption( thisForm, start, opt40 );
    start = 16;
    option2Good = checkOption( thisForm, start, opt100 );
    vtocheck = checkRate( thisForm.feerate );
    if ( ((option1Good) && (vtocheck == 40)) || ( (option2Good) && ( vtocheck == 100)) ){
    return true;
    }
    else
    alert("Not enough input.");
    return false;
    }

    function checkCheckBox(agreement) {
    if ( agreement.checked == true ){
    return true;
    }else
    alert('Please read and designate your Agreement to Terms of Service');
    return false;
    }


    function checkRate( rate ) {
    j=rate.length;
    for (a=0; a<j; a++){
    if(rate[a].checked) {
    //you found the rate
    return( rate[a].value );
    }
    }
    }



    This is working for me, though it doesn't seem to be all that efficient.
    I do have some questions about clearing the form and returning the focus etc. I'll let you digest this a little first
    Woodk.



Tags for this Thread