Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Member Be Loud Online's Avatar
    Join Date
    Apr 2011
    Location
    GTA, Ontario, Canada
    Posts
    53
    Member #
    27439
    Liked
    7 times
    I have created a questionnaire using PHP that totals the score of your answers. 1 point for yes, 0 points for no. If you score between 0 and 10 you are brought to landing page A, between 11 and 20 go to landing page B, etc, etc.

    Here is some of the code so you can see what I mean:


    HTML Code:

    Code:
    <form name="test" action="SelfTest.php" onsubmit="return validateForm()" method="post">
      <ol>
    <li>Do you brush your teeth daily with fluoridated toothpaste?<br />
      <input type="radio" value="1" name="no1">Yes &nbsp;&nbsp;<input type="radio" value="0" name="no1">No
    </li><br />
    What I am having trouble with is validating the answers. The result I am looking for is if a question is unanswered, the visitor will be prompted to go back and answer that particular question. "Question 4 was not answered. Please complete before submitting your results." kinda thing.

    The code I am using is not working and I can not figure out why. I am also very sure that there is a shorter and cleaner way of doing this. Any help is GREATLY appreciated...

    Here is the nonworking JavaScript shortened to the first 2 questions (there are 50 questions total):


    Code:

    Code:
    <script type="text/javascript">
    function validateForm()
    {
    if (document.forms["test"]["no1"].value==null || document.forms["test"]["no1"].value=="")
      {
      alert("Question 1 was not answered. Please complete before submitting.");
      return false;
      }
    else if (document.forms["test"]["no2"].value==null || document.forms["test"]["no2"].value=="")
      {
      alert("Question 2 was not answered. Please complete before submitting.");
      return false;
      }
    }
    </script>

  2.  

  3. #2
    Senior Member Webzarus's Avatar
    Join Date
    May 2011
    Location
    South Carolina Coast
    Posts
    3,322
    Member #
    27709
    Liked
    770 times
    From what I can see on my phone...

    JavaScript doesn't like double quotes in anything except text to be displayed in alerts.

    document.forms["test"]["no2"].value==null

    Should be

    document.forms.test.no2.value==null

    I think ( bad day today and it's not even close to being over )...

    Now for a question ... Do you have all 50 questions on the same page ? Or is it broken up in sections ?

    Broken up in sections would allow users to kinda not feel overwhelmed... As well... You could use scritping validation, score keeping, save scores ... If its broken into sections and you wrote their results to a DB .

    Maybe that's just me ..

    If I get a chance I'll test this JavaScript stuff out later when or if I get a few minutes at home.

  4. #3
    Member Be Loud Online's Avatar
    Join Date
    Apr 2011
    Location
    GTA, Ontario, Canada
    Posts
    53
    Member #
    27439
    Liked
    7 times
    Thanks Webzarus,

    I agree with you about breaking it up but this questionnaire is based of a print piece and resembles it in appearance. I will have to make your changes tomorrow as I am away from my "work" computer.

    Appreciate your help!

  5. #4
    Member Be Loud Online's Avatar
    Join Date
    Apr 2011
    Location
    GTA, Ontario, Canada
    Posts
    53
    Member #
    27439
    Liked
    7 times
    Thanks Webzarus, unfortunately your changes didn't solve the issue. Although I think I realize WHAT the issue is now... If you will notice in the html there are 2 radio inputs for value no1. Yes gives it a point of 1, no gives it a point of 0. BUT the visit can only answer yes OR no, which will always leave one of the radios responding in Javascript as null or void. I can't think of away around this so will have to validate through PHP instead. It will take longer to notify the visitor of their oversight, but I can't think of anyway around this.

    If you have a work around using Javascript, please let me know.

    Thanks again!


Remove Ads

Posting Permissions

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

Search tags for this page

how to validate a multiple radio button in javascript

,
how to validate a radio button and add score in html
,
how to validate null radio buttons in javascript
,
yes no radio button validation in javascript
Click on a term to search for related topics.
All times are GMT -6. The time now is 06:25 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com