SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast johnmanoahs's Avatar
    Join Date
    Jul 2004
    Location
    India
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Global scope for varaibles and arrays in Javascript.. please help me ASAP

    var isCorrect = new Array();

    function calc(qNo)
    {
    res1=document.form1.r1[0].checked;
    res2=document.form1.r1[1].checked;
    res3=document.form1.r1[2].checked;




    if(res1)
    {
    userAns[qNo]=document.form1.r1[0].value;
    }
    if(res2)
    {
    userAns[qNo]=document.form1.r1[1].value;
    }
    if(res3)
    {
    userAns[qNo]=document.form1.r1[2].value;
    }

    if(userAns[qNo] == answer[qNo])
    {

    isCorrect[qNo]=1
    }
    else
    isCorrect[qNo]=0

    }

    let me explain the whole program to you.

    Im creating an assessment engine. Every page contains a question followed by three options (radio buttons). The above function accepts a qNo which is the page no of the respective page (e.g. page1.html,page50.html). res1,res2,res3 is are variables holding the checked or unchecked status of the radio button. If checked the value of the radio box is saved in the array 'userAns'. For e.g.,if the qNo is '5' and the right answer is option 1, then calc(5) would assign 'document.form1.r1[0].value' to userAns[5]. If this userAns[5] is equal to anwer[5](which is already defined inside the js file), then isCorrect[5]=1 or else isCorrect[5]=0. This is to find out whether the user has given the right answer or not. Likewise in everypage, the isCorrect[qNo] is updated to either '0' or '1'. At the end of the test, the result is displayed in a newpage along with a feedback for wrong answers. At this page, the function is:

    for(i=1;i<=MAX_QUEST;i++)
    {

    if(isCorrect[i]==0)
    {
    document.write('Q.No.'+i+' You did not answer this correct'+'<br>')
    document.write(feedBack[i]+'<br>');
    }
    else
    {
    document.write('Q.No.'+i+' You answered this question correct'+'<br>');
    }
    }

    This function writes the feedback only for the wrong answers. But the problem is that the isCorrect[qNo] in the last page showing as 'undefined'.

    Also for every correct answer, a global variable called 'score' is incremented by 10. But at the last page, the value of score still remains '0' even if all questions are correct.

    Can u please suggest a way to retain the value of arrays and variables in everypage?

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Durham
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure I'm getting this. You've created a bunch of variables etc in one page and are tryng to pass them to other pages. It's important to remember that the nature of the web is that it is stateless (in serivng HTML docs) i.e. there is no real relationship between pages being served - your information is not stored anywhere.

    You can, of course, store it in places such as cookies or passing it in the URL string. However, you'd probably better achieve what you are trying to do using PHP.

    You can do what you are trying to achieve using just javascript, but not how you are currently. Variables being global are global to things within the page the user is viewing and not accessible by other pages unless some intermediary way is used.

    I hope I've read what you were trying to do right, otherwise ignore me If I have, and you'd like to do this with javascript, I'd be happy to help. I'd recommend using PHP (if it's available) and just posting to each subsequent page...


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
  •