Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function saveInput() {
  2.     //initialize variables to be stored in the local storage
  3.     var createName = [];
  4.     var createRoom = [];
  5.     var createClub = [];
  6.     var createGrade = [];
  7.     var createGender = [];
  8.     var createAnnouncement = [];
  9.    
  10.    
  11.     //check to see if there are existing announcements in the local storage by simply checking one of the arrays (only one array needs to be checked because if one array contains information, the rest of the arrays will also have information)
  12.     if (JSON.parse(localStorage.getItem("storeName")) != null) {
  13.    
  14.         // if the local storage isnt empty, assign old values from local storage to arrays
  15.         createName = JSON.parse(localStorage.getItem("storeName"));
  16.         createRoom = JSON.parse(localStorage.getItem("storeRoom"));
  17.         createClub = JSON.parse(localStorage.getItem("storeClub"));
  18.         createGrade = JSON.parse(localStorage.getItem("storeGrade"));
  19.         createGender = JSON.parse(localStorage.getItem("storeGender"));
  20.         createAnnouncement = JSON.parse(localStorage.getItem("storeAnnouncement"));
  21.     }
  22.    
  23.    
  24.    
  25.     //create new variables to store new data to later be put into the arrays
  26.     var newName = document.getElementById("pickName").value;
  27.     var newRoom = document.getElementById("pickRoom").value;
  28.     var newClub = document.getElementById("pickClub").value;
  29.     var newGrade = document.getElementById("pickGrade").value;
  30.     var newGender = document.getElementById("pickGender").value;
  31.     var newAnnouncement = document.getElementById("pickAnnouncement").value;
  32.    
  33.    
  34.     //to ensure the announcement can only be submitted if all fields are filled out, check each field is filled out
  35.     if (newName == "" || newRoom == "" || newClub == "" || newGrade == "" || newGender == "" || newAnnouncement == "")
  36.         alert("Please fill out all fields before submitting.");
  37.    
  38.     //if all fields are filled out, add the new data to the existing array
  39.     else {
  40.         createName.push(newName);
  41.         createRoom.push(newRoom);
  42.         createClub.push(newClub);
  43.         createGrade.push(newGrade);
  44.         createGender.push(newGender);
  45.         createAnnouncement.push(newAnnouncement);
  46.    
  47.    
  48.         //save variable to local storage
  49.         localStorage.setItem("storeName", JSON.stringify(createName));
  50.         localStorage.setItem("storeRoom", JSON.stringify(createRoom));
  51.         localStorage.setItem("storeClub", JSON.stringify(createClub));
  52.         localStorage.setItem("storeGrade", JSON.stringify(createGrade));
  53.         localStorage.setItem("storeGender", JSON.stringify(createGender));
  54.         localStorage.setItem("storeAnnouncement", JSON.stringify(createAnnouncement));
  55.  
  56.         //test to see arrays are saved properly
  57.         alert(createName);
  58.         alert(createRoom);
  59.         alert(createClub);
  60.         alert(createGrade);
  61.         alert(createGender);
  62.         alert(createAnnouncement);
  63.         window.location.href = "./mainPage.html";
  64.     }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement