SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: an Odd problem

  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    an Odd problem (updated with more code)

    Hello all,

    This is my code:

    <select name="TCM_TCF_Code" ID="TCM_TCF_Code" Onchange="validate_Field_code('1', this.value);">
    <option Selected value="ADAP">ADAP
    <option value="ADCN">ADCN
    <option value="ADCT">ADCT
    <option value="ADCY">ADCY
    <option value="ADL1">ADL1
    <option value="ADL2">ADL2
    <option value="ADST">ADST
    <option value="ADSU">ADSU
    <option value="ADZP">ADZP
    <option value="FAXS">FAXS
    <option value="MOBL">MOBL
    <option value="PROV">PROV
    <option value="TELE">TELE
    </select>
    <input type="Text" name="TCM_TCF_Code_decode" ID="TCM_TCF_Code_decode" disabled>

    In Javascript:


    function validate_Field_code(passed_index, passed_value){

    var javascript_dropdown_array = new Array(1,13,2);
    alert(passed_value)
    javascript_dropdown_array[1,1,1]='ADAP';
    javascript_dropdown_array[1,1,2]='Address Apartment';
    javascript_dropdown_array[1,2,1]='ADCN';
    javascript_dropdown_array[1,2,2]='Address Country';
    javascript_dropdown_array[1,3,1]='ADCT';
    javascript_dropdown_array[1,3,2]='Address City';
    javascript_dropdown_array[1,4,1]='ADCY';
    javascript_dropdown_array[1,4,2]='Address County';
    javascript_dropdown_array[1,5,1]='ADL1';
    javascript_dropdown_array[1,5,2]='Address Line 1';
    javascript_dropdown_array[1,6,1]='ADL2';
    javascript_dropdown_array[1,6,2]='Address Line 2';
    javascript_dropdown_array[1,7,1]='ADST';
    javascript_dropdown_array[1,7,2]='Address State';
    javascript_dropdown_array[1,8,1]='ADSU';
    javascript_dropdown_array[1,8,2]='Address Suite Number';
    javascript_dropdown_array[1,9,1]='ADZP';
    javascript_dropdown_array[1,9,2]='Address Zip Code';
    javascript_dropdown_array[1,10,1]='FAXS';
    javascript_dropdown_array[1,10,2]='Fax Numbers';
    javascript_dropdown_array[1,11,1]='MOBL';
    javascript_dropdown_array[1,11,2]='Mobile / Cell Nos.';
    javascript_dropdown_array[1,12,1]='PROV';
    javascript_dropdown_array[1,12,2]='Province';
    javascript_dropdown_array[1,13,1]='TELE';
    javascript_dropdown_array[1,13,2]='Telephone Numbers';
    alert("in validate_field_Code javascript function!!!!!!!!!!!!!!!!! "+passed_index)
    junk_kounter = 13;
    for (itel = 1; itel < 13; itel++) {

    alert(" in the do loop !!!!!!!!!! value of itel -- "+itel)
    alert(" dropdown array content---> "+javascript_dropdown_array[passed_index,itel,1])
    alert(" ---code from the form " + passed_value)

    if (javascript_dropdown_array[passed_index,itel,1] = passed_value) {
    alert(" in the if statement!!!!!!!!!! "+itel+" uttam---> "+javascript_dropdown_array[passed_index,itel,2])
    document.add_an_entry_standard.TCM_TCF_Code_decode.value=javascript_dropdown_array[passed_index,itel,2]
    i=junk_kounter;
    }
    }
    }

    --------------------

    I pass the value 1 in the passed_index and ADCN for the passed_value.

    I still get "Telephone Numbers" as the value put in document.add_an_entry_standard.TCM_TCF_Code_decode.value

    as per this statement:

    document.add_an_entry_standard.TCM_TCF_Code_decode.value=javascript_dropdown_array[passed_index,itel,2]

    I am at a loss to know what in H*ck is happening. Any suggestions please?

    Thanks in advance.

    Uttam

    PS: The alerts are only to help me debug and will be deleted once the code runs correctly.
    Last edited by u0107; Mar 17, 2006 at 00:17.

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The problem you are having is with the way you're trying to create multi-dimensional arrays.

    The line javascript_dropdown_array=new Array(1,13,2) does not declare a multi-dimensional array, but rather a single array containing the values 1, 13 and 2. Passing more than one argument into the new Array() method declares a one dimensional array with the aurguments as values in the array. Only if you pass a single argument in will that argument be used to size the array.

    Because javascript does not really support multi-dimensional arrays, you cannot declare a multi-dimensional array in one step. Instead you have to declare an array of arrays, but this means you have to iterate through the first array and declare it's elements to be an array too.

    Similarly when you reference elements in a multi-dimensional array you cannot pass in multiple arguments, you have to reference the first element, and then any sub elements.

    The following should do what you're looking for I think (although I'm not sure why you've declared a 3-Dimensional array when you are only really using 2 dimensions?

    function validate_Field_code(passed_index, passed_value){

    var javascript_dropdown_array = new Array(1);
    javascript_dropdown_array[1]=new Array(13);
    for(var i=1; i<=13; i++){
    javascript_dropdown_array[1][i]=new Array(2);
    }
    alert(passed_value);
    javascript_dropdown_array[1][1][1]='ADAP';
    javascript_dropdown_array[1][1][2]='Address Apartment';
    javascript_dropdown_array[1][2][1]='ADCN';
    javascript_dropdown_array[1][2][2]='Address Country';
    javascript_dropdown_array[1][3][1]='ADCT';
    javascript_dropdown_array[1][3][2]='Address City';
    javascript_dropdown_array[1][4][1]='ADCY';
    javascript_dropdown_array[1][4][2]='Address County';
    javascript_dropdown_array[1][5][1]='ADL1';
    javascript_dropdown_array[1][5][2]='Address Line 1';
    javascript_dropdown_array[1][6][1]='ADL2';
    javascript_dropdown_array[1][6][2]='Address Line 2';
    javascript_dropdown_array[1][7][1]='ADST';
    javascript_dropdown_array[1][7][2]='Address State';
    javascript_dropdown_array[1][8][1]='ADSU';
    javascript_dropdown_array[1][8][2]='Address Suite Number';
    javascript_dropdown_array[1][9][1]='ADZP';
    javascript_dropdown_array[1][9][2]='Address Zip Code';
    javascript_dropdown_array[1][10][1]='FAXS';
    javascript_dropdown_array[1][10][2]='Fax Numbers';
    javascript_dropdown_array[1][11][1]='MOBL';
    javascript_dropdown_array[1][11][2]='Mobile / Cell Nos.';
    javascript_dropdown_array[1][12][1]='PROV';
    javascript_dropdown_array[1][12][2]='Province';
    javascript_dropdown_array[1][13][1]='TELE';
    javascript_dropdown_array[1][13][2]='Telephone Numbers';

    alert("in validate_field_Code javascript function!!!!!!!!!!!!!!!!! "+passed_index)
    junk_kounter = 13;
    for (itel = 1; itel < 13; itel++) {

    alert(" in the do loop !!!!!!!!!! value of itel -- "+itel)
    alert(" dropdown array content---> "+javascript_dropdown_array[passed_index,itel,1])
    alert(" ---code from the form " + passed_value)

    if (javascript_dropdown_array[passed_index][itel][1] = passed_value) {
    alert(" in the if statement!!!!!!!!!! "+itel+" uttam---> "+javascript_dropdown_array[passed_index][itel][2]);
    document.add_an_entry_standard.TCM_TCF_Code_decode.value=javascript_dropdown_array[passed_index][itel][2];
    i=junk_kounter;
    }
    }
    }

    Hope this helps,

    NickJ

  3. #3
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Nick... this worked.

    To answer why I have a three dimensional array.

    The code that I displayed is the "View source" of a page generated by an ASP script which reads a back end Access database. The dropdown contains the key value while the decode is what is displayed from the database table.

    I can have multiple such dropdown objects on a single form - the passed_index variable keeps track of how many and where to look.

    In the example that I sent in, it was the simplest version where only dropdown object was on the form. If there were three dropdown objects with a max of 40 lookup values in say the second dropdown, then the javascript_dropdown_array would be dimensioned as javascript_dropdown_array[3][40][2]

    and the there would be 120 maximum code / decode pairs.

    With your help, I have actually achieved lookup of an access table within ASP / Javascript something many were telling me was not possible.

    Thank you very much!

    Uttam


Bookmarks

Posting Permissions

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