0

I creating a quiz website with 2 questions for now. User will be provided a question and radio buttons to select the answer. Whatever the user selects will be stored in a cookie which will later be provided to php for database retrieval.status array is used to check whether first option was checked or second. When I select the previous button the previous question gets diplayed but the radio button does not change that is it will be same as the second question.

<html>

<head>
    <script>
        var count = 1;
        var status = new Array();

        function calculateInput() {
            if (document.getElementById('radio1').checked) {
                status[count] = 1;
                ans1_value = document.getElementById('radio1').value;
                document.cookie = count + "=" + ans1_value + ";"



            } else
            if (document.getElementById('radio2').checked) {
                status[count] = 2;
                ans1_value = document.getElementById('radio2').value;
                document.cookie = count + "=" + ans1_value + ";"


            }
        }

        function myFunction() {


            count = count + 1;
            if (count == 2) {
                x = document.getElementById("questions"); // Find the element
                x.innerHTML = "Your PR firm has been hired to promote the reopening of a historic luxury hotel in your town. In addition to producing the opening night gala event, your team will be creating a theme for the evening. Would you rather:"; // Change the content
                document.getElementById('Answer0').firstChild.nodeValue = 'Discover more about the hotel’s history to develop the theme. (Attention to detail)';
                document.getElementById('Answer1').firstChild.nodeValue = 'Research how the renovation team preserved the building’s original details for inclusion in the event’s press kit. (Research)';
                document.getElementById("radio1").checked = true;
                document.getElementById("radio1").value = "321";
                document.getElementById("radio2").value = "311";
            } else if (count > 2) {
                window.location.href = "http://localhost/quiz/same_page_implementation/final.php";
            }
        }

        function calculatePrev() {
            count = count - 1;
            if (count == 1) {
                x = document.getElementById("questions"); // Find the element
                x.innerHTML = "You are a project manager in an architecture firm that’s designing the world’s largest Ferris Wheel—one that will tower over the São Paulo skyline. Which of the following tasks would you most want to handle?"; // Change the content
                document.getElementById('Answer0').firstChild.nodeValue = 'Meeting with the project’s leaders weekly to ensure safety requirements and budget targets are being met. (Planning)';
                document.getElementById('Answer1').firstChild.nodeValue = 'Meeting each week with the project’s world-famous architect to brief him on the team’s  progress. (Administration)';
                if (status[count] == 1) {
                    document.getElementById("radio1").checked = true;
                } else if (status[count] == 2) {
                    document.getElementById("radio2").checked = true;
                }


                document.getElementById("radio1").value = "325";
                document.getElementById("radio2").value = "310";
            }
        }
    </script>
</head>

<body>
    <p id="questions">
        You are a project manager in an architecture firm that’s designing the world’s largest Ferris Wheel—one that will tower over the São Paulo skyline. Which of the following tasks would you most want to handle?
    </p>

    <input type="radio" name="question1" value="325" id="radio1">
    <label for="radio1" id="Answer0">Meeting with the project’s leaders weekly to ensure safety requirements and budget targets are being met. (Planning)</label>
    <br>
    <input type="radio" name="question1" value="310" id="radio2">
    <label for="radio2" id="Answer1">Meeting each week with the project’s world-famous architect to brief him on the team’s progress. (Administration)</label>
    <br>


    <button type="button" onclick=" calculateInput(); myFunction()">Next</button>
    <button type="button" onclick=" calculatePrev()">Previous</button>

</body>

</html>
2
  • You are making this much too complicated. I'd do away with the cookies and just get the information on submit, either get or post. Commented Mar 23, 2014 at 16:02
  • but I do not want the whole page to reload when the user submits an answer. Commented Mar 23, 2014 at 18:07

1 Answer 1

1

rename "status" array to any other name. The problem is: when you write

var status = []

it is equally to window.status = [].

Window.status is http://www.w3schools.com/jsref/prop_win_status.asp. It's disabled in most browsers but still exist and can't be overwritten.

I've tried status1 instead and it seems working.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.