SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple HTML JScript Form

    Hi,

    I'm currently building a website which requires the user fill out a small questionnaire via html <form> code, and then displays a tally of the results (i.e., 10 questions which require yes or no answers). I know how to setup the form in HTML, it's just the code required to calculate the results, and then display them on the same page which I'm unsure about. Any help is appreciated!

    Thanks,

    Christian

  2. #2
    SitePoint Evangelist vikrantkorde's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai, India
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Christian,

    could you please just give the sample sniplet of the form, it will be helpful in giving the solution.
    Vikrant Korde
    S Y S T I M E, Mhape,
    Mumbai, Maharashtra, India.

  3. #3
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Certainly. Sorry, I should've pasted it with my query.

    <form name="form_support_team" method="post" action="">
    ...
    <label>
    <input type="radio" name="question_1" value="yes">Yes
    </label>
    <label>
    <input type="radio" name="question_1" value="no">No
    </label>
    ...
    </form>

    There are ten questions in total. I just need to know the code to, once the form is submitted, calculate the results (and if possible, display them on the same page without needing to refresh).

    Thanks,

    Christian

  4. #4
    SitePoint Evangelist vikrantkorde's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai, India
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use something like this.
    1) create 2 variables, one for number of Yes and another for number of No
    var noOfYes = 0;
    var noOfNo = 0;
    2) get the object by its name
    var arrRadObjs = document.getElementsByName("question_1");
    3) Check what is selected. and increase the respective counter.
    if(arrRadObjs[0].checked){
    noOfYes = noOfYes + 1;
    } else {
    noOfNo = noOfNo + 1;
    }
    4) repeat step 3 for all the radio buttons
    5) at the end you will get number of yes, number of no and the total number of questions.
    6) find the ratio, give the result. without refreshing the page.

    you can also cut down variables to 1 as you can get
    total number of questions = number of Yes + number of No
    Vikrant Korde
    S Y S T I M E, Mhape,
    Mumbai, Maharashtra, India.

  5. #5
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks a lot for the help. I'm a bit puzzled though; how do I implement this into the html code? Do I store it in a separate file, or put each of step 3 directly under each radio button? How would I do this?

    Thanks,

    Christian

  6. #6
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you tally the results, are you wanting to tally the results from all users that have ever used the form? That can be done, but you'll need some place to record previous uses of the form, like a database. It may be better to POST to a PHP, Perl or ASP script that can write out a file or update a database with the results, instead of using Javascript.

  7. #7
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Well, database access is a bit uncertain at the moment. I'd prefer to use PHP personally, but if it's possible to do this in javascript, it'd save a bit of hassle. The tally is just for any user, each time they complete the form.


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
  •