1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Rep Power

    Question Looping through value assignments in Javascript for BIG form??

    I have been working on this BIG form, and I need to know if I can use a Loop to assing values ... etc

    This is original working form which recursively strips out values
    from a file field and gets the file name, next it stores the values to
    another form

    function doUpdateSubmit()

    var _fullPath1 = document.frmFiles.FILE1.value;
    var _fullPath2 = document.frmFiles.FILE2.value;
    var _fullPath3 = document.frmFiles.FILE3.value;
    var _fullPath4 = document.frmFiles.FILE4.value;
    var _fullPath5 = document.frmFiles.FILE5.value;
    var _fullPath6 = document.frmFiles.FILE6.value;
    var _fullPath7 = document.frmFiles.FILE7.value;
    var _fullPath8 = document.frmFiles.FILE8.value;

    var _new_filename1 = _fullPath1.lastIndexOf("\\");
    var _fileName1 = _fullPath1.substring( _new_filename1+1, _fullPath1.length);

    var _new_filename2 = _fullPath2.lastIndexOf("\\");
    var _fileName2 = _fullPath2.substring( _new_filename2+1, _fullPath2.length);

    var _new_filename3 = _fullPath3.lastIndexOf("\\");
    var _fileName3 = _fullPath3.substring( _new_filename3+1, _fullPath3.length);

    var _new_filename4 = _fullPath4.lastIndexOf("\\");
    var _fileName4 = _fullPath4.substring( _new_filename4+1, _fullPath4.length);

    var _new_filename5 = _fullPath5.lastIndexOf("\\");
    var _fileName5 = _fullPath5.substring( _new_filename5+1, _fullPath5.length);

    var _new_filename6 = _fullPath6.lastIndexOf("\\");
    var _fileName6 = _fullPath6.substring( _new_filename6+1, _fullPath6.length);

    var _new_filename7 = _fullPath7.lastIndexOf("\\");
    var _fileName7 = _fullPath7.substring( _new_filename7+1, _fullPath7.length);

    var _new_filename8 = _fullPath8.lastIndexOf("\\");
    var _fileName8 = _fullPath8.substring( _new_filename8+1, _fullPath8.length);

    //Assign values to Allot of stuff for submittion to database
    document.frmUpdateAll.itemimage1.value = _fileName1;
    document.frmUpdateAll.itemimage2.value = _fileName2;
    document.frmUpdateAll.itemimage3.value = _fileName3;
    document.frmUpdateAll.itemimage4.value = _fileName4;
    document.frmUpdateAll.itemimage5.value = _fileName5;
    document.frmUpdateAll.itemimage6.value = _fileName6;
    document.frmUpdateAll.productFile.value = _fileName7;
    document.frmUpdateAll.prodImage.value = _fileName8;


    I want to change it to something like this..... I may not be using the correct method to do this....

    This is not working

    function dothis()

    var _usrID = document.frmAdd.usrID.value;

    var FILE = new Array("FILE1","FILE2","FILE3","FILE4","FILE5","FILE6");
    var image = new Array();
    var _fileName = new Array();
    var _fullPath = new Array();
    var _new_filename = new Array();
    var a=0;

    for (var i=1; i<6; i++)

    /************* Just some tests
    alert ( i );
    alert (FILE[a]);
    var dod = FILE[a];
    var aa = "document.frmAdd.";
    var bb = ".value";
    var cc = "document.frmData.";
    var test = aa+FILE[a]+bb;
    alert (test);

    alert (a);
    _fullPath[a] = document.frmAdd.FILE[a].value;

    alert (_fullPath[a]);

    //Stripping filename
    _new_filename[a] = _fullPath[a].lastIndexOf("\\");
    _fileName[a] = _fullPath[a].substring( _new_filename[a]+1, _fullPath[a].length);

    alert (_fileName[a]);

    //Assign values to Allot of stuff for submittion to database

    document.frmData.image[a].value = _fileName[a];


    I am submitting these two forms to two different places, the original form uploads the files using CGI script and once I have the filenames using this function I assign those file names to another form and submit that to write to the database.

    Any Help in this is appreciated.... This would be a learning curve for me...
  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Sacramento, CA
    Rep Power
    Looks ok. A couple things.
    Rewrite your for loop like this:
    for ( a = 0; a < 6; a++ ) {
    and you can make your code a little simpler. Also, the way it is now you're going to miss one of your file uploads.

    Also, this line
    _fullPath[a] = document.frmAdd.FILE[a].value;
    should probably be
    _fullPath[a] = eval( "document.frmAdd.FILE" + (a+1) + ".value");
    I don't think the way you're doing it would work (I could be wrong, so don't listen to me if that part is working ).

    Is there anything specific about your code that isn't working right?
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2003
    Rep Power
    Ok, let me get this straight: For each value, you are...
    1) setting a variable equal to document.frmFiles.FILE{n}.value
    2) finding the last occurrence of a slash in the filename
    3) extracting the portion of the filename after the last slash
    4) setting the value of document.frmUpdateAll.{somename}{n}.value equal to the shortened filename

    This could easily be done in a loop, without even using arrays.

    var prevElemInFrmFiles = 0;
    var prevElemInFrmUpdateAll = 0;
    var fullpath, filename;
    for (var i = 0; i < 8; i++) {
      fullpath = document.frmFiles.elements[i + prevElemInFrmFiles].value;
      filename = fullpath.substring(fullpath.lastIndexOf("\\") + 1);
      document.frmUpdateAll.elements[i + prevElemInFrmUpdateAll].value = filename;
    Notice the two variables, prevElemInFrmFiles and prevElemInFrmUpdateAll. I have set them both to zero. What they represent is the number of form elements that are "before" the 8 elements that you are reading/writing. As you go through the HTML code for the form, each element is given an integer index beginning with zero and increasing. The first element is 0, the second is 1, etc. So if you have no elements before the text entries, just leave it to zero. If you have elements in-between the entries, for example,
    text entry.... browse button
    text entry.... browse button
    text entry.... browse button
    just multiply i by the number of elements per group (1 text entry + 1 browse button = 2), for example (there would be 2 places that would need you to do this):
    fullpath = document.frmFiles.elements[i*2 + prevElemInFrmFiles].value;
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Rep Power


    Thanx for your responce guyz,

    bricker42... to answer your question I only posted part of this form, there is actually attribute elements to each FILE field, that I am going to tie into this loop, like if its a image file I have a field with Height and Width for that FILE etc.

    So this is actually a bigger script than it looks right now, so in a loop it looks simpler and neat. SO the field names are like this

    FILE1 (this is first file uploaded, and I strip out the file name along with its extension and save it to db.)
    Then I have


    So you see this is one set of fields. that can go up to n'th fields, I need to automate this because this is the second appication I am working on that uses the same layout etc.

    So Clone53421 ! I hope that answers your question.

    I think eval() would work, I was having problem constructing the part

    _fullPath[a] = document.frmAdd.FILE[a].value;

    where instead of value passed from the form, it was showing document.frmAdd.FILE1.value in there...

    it should have been something like C:\path to file\filename.jpg

    so thats where I was stuck but I think eval() is what I needed and I will try that and get back to you guyz.


IMN logo majestic logo threadwatch logo seochat tools logo