Bob
Bob

Reputation: 91

Javascript--How to store parsed query string into a cookie

I'm working on a project in where I have to take info from page 1 (name, id, and semester) and store it in a query string. Then I have to take that info and store it a cookie in page 2, and display it on page 3. I've stored it in a query string, correctly, I believe. I've set it (for now) as an alert message, just to make sure it's working. The alert displays on page 2 (Success!!!). How do I store this parsed information into a cookie, so I can display on page 3? I need to store and display both Page 1 and Page 2's info and display it on page 3.

Function Page 1:

function querySemester() {
        var saveSemester = location.search;
        var semesterData = "";
        if (saveSemester != "")
            semesterData = saveSemester.substring(saveSemester.search("&FirstName"), saveSemester.length);
        saveSemester = "?semester=" + semesterDisplay;
        location.href = "RegistrationPage_2.html" + saveSemester;

Functions Page 2:

function submitRegistration() {
        var course = document.registration.courses.value;
        var section = document.registration.section.value;
        var major = document.registration.needForMajor.value;
        //To display major requirement in confirm message
        var checkDisplay;
        if (document.registration.needForMajor.checked == true) {
            checkDisplay = "Course Needed For Major";
        }
        else {
            checkDisplay = "";
        }
        //Validates course
        if (course == "") {
            window.alert("You must select a Course!");
            return false;
        }
        //Validates section
        if (section == "") {
            window.alert("You must select a Section!");
            return false;
        }
        // Validates that the course and section are compatible
        var error = true;
        switch (course) {
            case "CIS 100":
                if (section == '100001') {
                } else if (section == '100gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 100!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 120":
                if (section == '120001') {
                } else if (section == '120gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 120!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 220":
                if (section == '220001') {
                } else if (section == '220gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 220!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 299":
                if (section == '299001') {
                } else if (section == '299gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 299!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 302":
                if (section == '302gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 302!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 304":
                if (section == '304001') {
                } else if (section == '304gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 304!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 321":
                if (section == '321001') {
                } else if (section == '321gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 321!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 322":
                if (section == '322gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 322!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 325":
                if (section == '325gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 325!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 330":
                if (section == '330001') {
                } else if (section == '330gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 330!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 332":
                if (section == '332001') {
                } else if (section == '332gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 332!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 341":
                if (section == '341001') {
                } else if (section == '341gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 341!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 343":
                if (section == '34301a') {
                } else if (section == '34301b') {
                } else {
                    window.alert("You must select a valid section for CIS 343!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 352":
                if (section == '352gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 352!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 354":
                if (section == '354001') {
                } else if (section == '354gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 354!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 401":
                if (section == '401gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 401!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 419":
                if (section == '419x01') {
                } else {
                    window.alert("You must select a valid section for CIS 419!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 490":
                if (section == '490001') {
                } else if (section == '490gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 490!");
                    error = false;
                    return false;
                }
                break;
            case "CIS 492":
                if (section == '492gw1') {
                } else {
                    window.alert("You must select a valid section for CIS 492!");
                    error = false;
                    return false;
                }
                break;
            case "MAT 195":
                if (section == '195001') {
                } else if (section == '195w01') {
                } else {
                    window.alert("You must select a valid section for MAT 195!");
                    error = false;
                    return false;
                }
                break;
            case "MAT 215":
                if (section == '215001') {
                } else if (section == '215w01') {
                } else {
                    window.alert("You must select a valid section for MAT 215!");
                    error = false;
                    return false;
                }
                break;
            case "MAT 225":
                if (section == '225001') {
                } else if (section == '225w01') {
                } else {
                    window.alert("You must select a valid section for MAT 225!");
                    error = false;
                    return false;
                }
                break;
            case "MAT 281":
                if (section == '281001') {
                } else if (section == '281w01') {
                } else {
                    window.alert("You must select a valid section for MAT 281!");
                    error = false;
                    return false;
                }
        }
            var querySemester = location.search;
            querySemester = querySemester.substring(1, querySemester.length);
            var queryArray = querySemester.split("&");
            for (var i = 0; i < queryArray.length; ++i) {
                window.alert(queryArray[i] + "<br />");
            }
                return true;
    }
    function queryInfo() {
        var saveInfo = location.search;
        var data = "";
        if (saveInfo != "")
            data = saveInfo.substring(saveInfo.search("&FirstName"), saveInfo.length);
        saveInfo = "?semester=" + semesterDisplay;
        location.href = "GreendaleVerification.html" + saveInfo;
    }

Upvotes: 1

Views: 685

Answers (1)

ajc2000
ajc2000

Reputation: 834

Since cookies don't always work when you're developing locally (I haven't been able to use them in Chrome when developing in the past), I'd recommend localStorage:

This is how you set a value in localStorage:

localStorage.setItem("<attribute>", "<data>");

And this is how you get a value from localStorage:

var data = localStorage.getItem("<attribute>");
//code to do something with data

So, in the case of your code, you may want to store the data in your code on Page 1 like so:

localStorage.setItem("data", semesterData);
//it seemed like the data you passed to the URL was in semesterData, but
//you can pass any JavaScript string into the localStorage.setItem() command

And then obtain it on Page 2 and page 3 with this command:

var data = localStorage.getItem("data");

Upvotes: 1

Related Questions