www.webdeveloper.com
Results 1 to 9 of 9

Thread: Counter not working?

  1. #1
    Join Date
    Mar 2009
    Posts
    17

    Counter not working?

    I have a questionnaire with Radio buttons (HTML/Javascript). If any question is left out, an Alert message is supposed to display. When the questionnaire is completed, the Javascript is supposed to check the appropriate questions for a Yes, No and NA responses. In case of inappropriate answers, another Alert message pops up, otherwise the user moves on.

    Unfortunately, the first Alert message (regarding the questionnaire being incomplete) always shows; no matter what. I'm just stumped.

    Here is an example of a Yes question:
    <input type="radio" name="pt2[0]" value="" id="y[0]" onclick="javascript:If(document.part2.pt2[0].y[0].checked==true){CountIt();}"/>

    And the Javascript looks like:
    <script language="javascript">
    <!--
    var count=0;
    function CountIt() {
    count=count+1;
    return count;
    }

    function CheckBlank(count) { // Are there any unanswered questions?
    if(count==18) {
    CheckIt();
    }
    else {alert("Please answer every question. Thank you.");}

    }
    function CheckIt() { // Are the Yes, No, & NA questions respectively answered?

    pt2,y,n,na = new Array(18)

    for(i=0; i<=18; i++) {
    for(j=0; j<=18; j++) {
    for(k=0; k<=18; k++) {
    for(l=0; l<=18; l++) {
    switch (i,j,k,l) {
    case 0: case 1: case 2: case 6: case 11: case 13: case 14: case 15: case 16: case 17:
    if(document.part2.pt2[i].y[j].checked==false) {
    alert("We regret, but you cannot join at this time.");
    break;
    }
    case 3: case 4: case 5: case 8: case 9: case 10: case 12:
    if(document.part2.pt2[i].n[k].checked==false) {
    alert("We regret, but you cannot join at this time.");
    break;
    }
    case 8: case 9: case 10: case 11: case 12:
    if(document.part2.pt2[i].na[l].checked==false) {
    alert("We regret, but you cannot join at this time.");
    break;
    } // end if
    } // end for l
    } // end for k
    } // end for j
    } // end switch
    } // end for i
    }

    //-->
    </script>

    Will you please tell me what exactly is wrong here?

    Sincerely,
    Jakob
    Montreal

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,356

    Question

    You might want to rethink what you are trying to do:

    If you are using radio buttons, only one can be chosen at a time
    so you would not need to check for Yes, No, or NA. Could be counted in one loop.

    Also the following is invalid syntax for JS:
    switch (i,j,k,l) { // can only test one variable at a time

    Would not need to do this with design shift
    if(document.part2.pt2[i].n[k].checked==false) { // extremely complicated test

    Recommendation:
    1. Give more than one example of questions you are trying to survey.
    2. If certain questions disqualify person, indicate which are Yes, No or NA.

  3. #3
    Join Date
    Mar 2009
    Posts
    17
    Dear JMRKER,

    Thanks for your response. Shortly after I posted the original message, I realized where some mistakes were and I have since revised the code; unfortunately, it still doesn't work.

    Here is a typical radio button:
    <input type="radio" name="pt2[0]" value="" id="y[0]" onclick="javascript:If(document.part2.pt2[0].y[0].checked==true){CountIt();}"/>

    And the processing Javascript is:
    <script language="javascript">
    <!--
    var count=0;
    function CountIt() {
    count=count+1;
    return count;
    }

    function CheckBlank(count) { // Are there any unanswered questions?
    if(count==17) {
    CheckIt();
    }
    else {alert("Please answer every question. Thank you.");}

    }
    function CheckIt() { // Are the Yes, No, & NA questions respectively answered?

    var pt2,y,n,na = new Array(17);

    for(i=0; i<=17; i++) {
    switch (i) {
    case 0: case 1: case 2: case 6: case 11: case 13: case 14: case 15: case 16: case 17:
    if(document.part2.pt2[i].y[i].checked!=true) {
    alert("We regret, but you cannot join at this time.");
    break;
    }
    case 3: case 4: case 5: case 8: case 9: case 10: case 12:
    if(document.part2.pt2[i].n[i].checked!=true) {
    alert("We regret, but you cannot join at this time.");
    break;
    }
    case 8: case 9: case 10: case 11: case 12:
    if(document.part2.pt2[i].na[i].checked!=true) {
    alert("We regret, but you cannot join at this time.");
    break;
    } // end if
    } // end switch
    } // end for i
    }

    //-->
    </script>

    The Radio button code should work because it is in the general format:
    document.form name.element name.id.property. It worked in my previous questionnaire form. I'm still stumped. Any ideas?

    Sincerely,
    Jakob
    Montreal

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,356
    Two more requests ...

    1. Can you provide the HTML and JS associated with your problem (or a link to the live script) to evaluate fully.

    2. Easier to review you code if you place between the [ code] [ /code] tags (without spaces).

  5. #5
    Join Date
    Mar 2009
    Posts
    17
    Dear JMRKER,

    Thanks again for answering so promptly. Unfortunately, this site will not let me post the entire page (31,298 chars. as opposed to the max. allowed - 10000 chars.). So below, are the relevant bits:

    All of the radio input buttons look likethe index changes from 0 to 17)
    <CODE>
    <input type="radio" name="pt2[0]" value="" id="y[0]" onclick="javascript:If(document.part2.pt2[0].y[0].checked==true){CountIt();}"/>
    </CODE>

    The called Javascript is:
    <CODE>
    <script language="javascript">
    <!--
    var count=0;
    function CountIt() {
    count=count+1;
    return count;
    }

    function CheckBlank(count) { // Are there any unanswered questions?
    if(count==17) {
    CheckIt();
    }
    else {alert("Please answer every question. Thank you.");}

    }
    function CheckIt() { // Are the Yes, No, & NA questions respectively answered?

    var pt2,y,n,na = new Array(17);

    for(i=0; i<=17; i++) {
    switch (i) {
    case 0: case 1: case 2: case 6: case 11: case 13: case 14: case 15: case 16: case 17:
    if(document.part2.pt2[i].y[i].checked!=true) {
    alert("We regret, but you cannot join at this time.");
    break;
    }
    case 3: case 4: case 5: case 8: case 9: case 10: case 12:
    if(document.part2.pt2[i].n[i].checked!=true) {
    alert("We regret, but you cannot join at this time.");
    break;
    }
    case 8: case 9: case 10: case 11: case 12:
    if(document.part2.pt2[i].na[i].checked!=true) {
    alert("We regret, but you cannot join at this time.");
    break;
    } // end if
    } // end switch
    } // end for i
    }

    //-->
    </script>
    </CODE>

    The statement starting the whole process after the user answers the questions, is:
    <CODE>
    <p align="justify" class="MsoNormal">
    <o></o>
    Thank you for your co-operation.</p>
    <p align="justify" class="MsoNormal">Click <span class="GramE"><span style='color:red'><a href="#" target="_self" onclick="CheckBlank(count); return false;">Here</a></span></span> when you are finished with Part One and Part Two
    <o></o>
    . </p>
    </CODE>

    And finally, the whole thing is inside a form:
    <CODE>
    <form name="part2"action="quest.php" method="post">
    .
    .
    .
    </form>
    </CODE>

    The file quest.php is called to process certain text fields in a previous questionnaire (to this one), once the user qualifies (according to his answers).

    Just to recap: the first Alert message (the one when the count is not equal to 17) is always displayed (even when the questionnaire is entirely filled out). And I don't know if the switch statement works, either.
    Any help you can offer is greatly appreciated

    Sincerely,
    Jakob
    Montreal

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,356

    Lightbulb

    Save it as a .txt file to your computer then upload it as an attachment to the forum in the 'advanced' section.
    Readers can then download it if they choose and rename it .html, .js, etc.

    Also, it is:
    [ code] [ /code] (without spaces)
    not:
    <code></code>

    And
    And I don't know if the switch statement works, either
    Too many '}' character for sure. Remove after 'break'; except for the last.
    Possibly add to the switch
    default: alert('Invalid value for:'+i); break;
    } // last curly-bracket to complete the switch statement block
    Last edited by JMRKER; 03-12-2009 at 02:58 PM.

  7. #7
    Join Date
    Mar 2009
    Posts
    17
    Quote Originally Posted by JMRKER View Post
    Save it as a .txt file to your computer then upload it as an attachment to the forum in the 'advanced' section.
    Readers can then download it if they choose and rename it .html, .js, etc.

    Also, it is:
    [ code] [ /code] (without spaces)
    not:
    <code></code>

    And


    Too many '}' character for sure. Remove after 'break'; except for the last.
    Possibly add to the switch
    default: alert('Invalid value for:'+i); break;
    } // last curly-bracket to complete the switch statement block

    Dear JMRKER,

    Attached is a (Notepad) .txt file of the page in question.
    Also, can you put a Switch statement inside a for loop, like I have?

    Sincerely,
    Jakob
    Attached Files Attached Files

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,356

    Lightbulb Consider this ...

    Your last posted text file must have been saved with a non-ascii editor at one time as there were tons of errors in it. Too many for me to fix.

    So I took the guts of your displays and created a totally different entry form.
    It is not complete as I don't know some of the answers to some of your questions (like the legal parts),
    but take a look at it and see if it is something you want to pursue further.

    Main change, the responses are checked as entered so you don't need to count the buttons clicked or do so much convoluted if..then..else tests.

    Try it out with various responses and post back if you have questions about the design. The color/size/position choices is easy to change with the CSS.
    Attached Files Attached Files

  9. #9
    Join Date
    Mar 2009
    Posts
    17
    Quote Originally Posted by JMRKER View Post
    Your last posted text file must have been saved with a non-ascii editor at one time as there were tons of errors in it. Too many for me to fix.

    So I took the guts of your displays and created a totally different entry form.
    It is not complete as I don't know some of the answers to some of your questions (like the legal parts),
    but take a look at it and see if it is something you want to pursue further.

    Main change, the responses are checked as entered so you don't need to count the buttons clicked or do so much convoluted if..then..else tests.

    Try it out with various responses and post back if you have questions about the design. The color/size/position choices is easy to change with the CSS.

    Dear JMRKER,

    Thank you for your sample questionnaire. I found it a bit overwhelming, though. That shows you my level of expertise.
    In any case, included as an attachment, is the Dreamweaver CS3 file in question. You can use either Dreamweaver or IE7 to peruse it. There are no syntax errors, only a logical one. It's probably a simple thing. Have a look and tell me what you think.

    Sincerely,
    Jakob

    P.S. This forum does not allow .htm attachments, so I had to post another .txt, but this time I saved it as such using Dreamweaver CS3. Hopefully, it will be more legible (especially in IE7). If you want, I can send the original .htm Dreamweaver file to your personal E-Mail. That way you'll see that there aren't as many errors as you imagine.
    Attached Files Attached Files
    Last edited by Jakob; 03-13-2009 at 11:55 AM.

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