www.webdeveloper.com
Results 1 to 8 of 8

Thread: Need "For" loop to give single final action

  1. #1
    Join Date
    Jul 2007
    Posts
    3

    Need "For" loop to give single final action

    Due to a need to support Netscape 4.7, I'm locked into some very limited options for a project I'm working on.

    I have a list of book titles that could be numbered in the 100s depending on user choices in an online order application. For each book title, the user must indicate a format (there are five choices, ie: hardcover, large print...) and a quantity for each format. I need to validate the form to be sure each book has a quantity indicated for at least one format type. I am using a "For" loop to go through the 5 format choices for each book on the list. If any title fails to have a format choice, I am revealing a graphic error message next to that title. If all titles have a format chosen, I want to submit the form. I am having trouble finding a way to submit the form only at the completion of the validation effort. If the user has to correct more than one entry on the form, my script submits upon every correction that is made. I need help writing a function that submits only once.

    Here is what I am doing ATM:

    function ValidateBindings(){
    for(x = 1; x <= recordsReturned; x++){
    bindingHC = eval("document.interactiveForm.hardcover00" + x + ".value");
    bindingLP = eval("document.interactiveForm.largeprint00" + x + ".value");
    bindingUC = eval("document.interactiveForm.UCassette00" + x + ".value");
    bindingACD = eval("document.interactiveForm.ACD00" + x + ".value");
    bindingUCD = eval("document.interactiveForm.UCD00" + x + ".value");
    bindingGroup = bindingHC + bindingLP + bindingUC + bindingACD + bindingUCD;
    reminderImage = eval("document.images.arrow00" + x);

    if(bindingGroup == "") {
    reminderImage.src = displayReminder;
    } else {
    reminderImage.src = hideReminder;
    document.interactiveForm.submit();
    }
    }
    }

    The problem with the script above is that:
    1.) Netscape 4.7 does not seem to recognize the eval() method.
    2.) The form subits every time it goes through the loop.

    Any suggestions will be gladly tested.

    Thanks,
    Vermifuge

  2. #2
    Join Date
    Jun 2007
    Posts
    347
    First, we'll get rid of all the nasty evals:

    PHP Code:
    function ValidateBindings(){
        var 
    form document.interactiveForm;
        for(var 
    1<= recordsReturnedx++){
            
    bindingHC  form["hardcover00" x].value;
            
    bindingLP  form["largeprint00" x].value;
            
    bindingUC  form["UCassette00" x].value;
            
    bindingACD form["ACD00" x].value;
            
    bindingUCD form["UCD00" x].value;
            
    bindingGroup bindingHC bindingLP bindingUC bindingACD bindingUCD;
            
    reminderImage document.images["arrow00" x];

            if(
    bindingGroup == "") {
                
    reminderImage.src displayReminder;
            } else {
                
    reminderImage.src hideReminder;
                
    form.submit();
            }
        }


  3. #3
    Join Date
    Jun 2007
    Posts
    347
    The form submits every time it goes through the loop.
    That's because the statement is inside the loop. I think we may need to know more background, and the actual intent.

    <EDIT> Sorry. I haven't read your introduction yet !</EDIT>

  4. #4
    Join Date
    Jun 2007
    Posts
    347
    This should work:

    PHP Code:
    function ValidateBindings(){
        var 
    valid false;
        
    // If document.interactiveForm doesn't work for NS4, use
        //    document.forms.interactiveForm.elements 
        
    var form document.interactiveForm;
        for(var 
    1<= recordsReturnedx++){
            
    bindingHC  form["hardcover00" x].value;
            
    bindingLP  form["largeprint00" x].value;
            
    bindingUC  form["UCassette00" x].value;
            
    bindingACD form["ACD00" x].value;
            
    bindingUCD form["UCD00" x].value;
            
    bindingGroup bindingHC bindingLP bindingUC bindingACD bindingUCD;
            
    reminderImage document.images["arrow00" x];

            if(
    bindingGroup == "") {
                
    valid false;
                
    reminderImage.src displayReminder;
            } else {
                
    reminderImage.src hideReminder;
            }
        }
        if(
    valid)
            
    form.submit();

    BUT, if the form is being submitted with a SUBMIT button as it should (else no JS = no customer), then this will not cancel submission.

    <form ... onsubmit="return ValidateBindings(this)">

    PHP Code:
    function ValidateBindings(form){
        var 
    valid false;
        for(var 
    1<= recordsReturnedx++){
            
    bindingHC  form["hardcover00" x].value;
            
    bindingLP  form["largeprint00" x].value;
            
    bindingUC  form["UCassette00" x].value;
            
    bindingACD form["ACD00" x].value;
            
    bindingUCD form["UCD00" x].value;
            
    bindingGroup bindingHC bindingLP bindingUC bindingACD bindingUCD;
            
    reminderImage document.images["arrow00" x];

            if(
    bindingGroup == "") {
                
    valid false;
                
    reminderImage.src displayReminder;
            } else {
                
    reminderImage.src hideReminder;
            }
        }
        return 
    valid;


  5. #5
    Join Date
    Jul 2007
    Posts
    3
    Banana Ananda:
    Thank you so much. Adding the "valid" variable outside the FOR loop was a good solution to the problem -- with this exception: var valid = false; should read - var valid = true at which point the script works beautifully in current browsers. Unfortunately neither of your suggestions work in NS4. No errors occur, the browser just sits there inert and nothing happens.

    Does anyone else have a suggestion how I can access the form fields in NS4?

    Thanks,
    Vermifuge

  6. #6
    Join Date
    Jun 2007
    Posts
    347
    should read - var valid = true
    Oh yeah..

    I don't have an NS4 to test with, but I see absolutely no reason for this not to work. It's all basic stuff.

    Which version did you use ?
    Are you submitting with a submit button, and using the onsubmit event ?
    Does an alert fire if you place it in the top of the function ?

  7. #7
    Join Date
    Jul 2007
    Posts
    3
    I beg your pardon. I made a false assumption.
    Your method for accessing the form elements is working just fine in NS4.
    It now appears that NS4 is failing to change the source of my reminder image from a transparent spacer gif to an indicator arrow.
    I will have to investigate this further.

  8. #8
    Join Date
    Jun 2007
    Posts
    347
    Hmmm. Can't explain that one.

    Should just add that the variables, bindingHC etc should be made local (with var operator). A little better.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles