Guest User

common.js

a guest
Sep 8th, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  var users = localStorage.getItem("usersDB");
  2.   users = JSON.parse(users);
  3.   if (users === null)
  4.      var users = [];
  5.  
  6.   function Create() {
  7.     var person = JSON.stringify({
  8.       fullname: document.getElementById('fullname').value,
  9.       username: document.getElementById('username').value,
  10.       oracleid: document.getElementById('oracleid').value,
  11.       email: document.getElementById('email').value,
  12.       phone: document.getElementById('phone').value,
  13.       jobtitle: document.getElementById('jobtitle').value,
  14.       location: document.getElementById('location').value,
  15.       password: document.getElementById('newpassword').value
  16.     });
  17.     users = getJSON();
  18.     users.push(JSON.parse(person));
  19.     localStorage.setItem("usersDB", JSON.stringify(users));
  20.     postJSON(localStorage.getItem("usersDB"));
  21.     return true;
  22.   }
  23.  
  24.   function getJSON(){
  25.     var resp ;
  26.     var xhttp = new XMLHttpRequest();
  27.     xhttp.onreadystatechange = function() {
  28.         if (this.readyState == 4 && this.status == 200) {
  29.           resp = this.responseText;
  30.         }
  31.     };
  32.     xhttp.open("GET", "../json/users.json", false);
  33.     xhttp.send();
  34.     return JSON.parse(resp);
  35.   }
  36.  
  37.    function postJSON(data){
  38.      var xhttp = new XMLHttpRequest();
  39.     xhttp.onreadystatechange = function() {
  40.         if (this.readyState == 4 && this.status == 200) {
  41.            
  42.         }
  43.     };
  44.     xhttp.open("POST", "/jsonhandler" , true);
  45.     xhttp.setRequestHeader("Content-Type", "application/json");
  46.     xhttp.send(data);
  47.   }
  48.  
  49.    document.getElementById('btn-signup').addEventListener('click', Create);
  50.  /* function Edit() {
  51.     tblPersons[selected_index] = JSON.stringify({
  52.         ID: $("#txtID").val(),
  53.         Name: $("#txtName").val(),
  54.         Phone: $("#txtPhone").val(),
  55.         Email: $("#txtEmail").val()
  56.     });
  57.     localStorage.setItem("tblPersons", JSON.stringify(tblPersons));
  58.     alert("Los datos han sido editados"); //Mensaje de alerta
  59.     return true;
  60.   }
  61.  
  62.   function Delete() {
  63.     tblPersons.splice(selected_index, 1);
  64.     localStorage.setItem("tblPersons", JSON.stringify(tblPersons));
  65.     alert("Persona Eliminada");
  66.   }*/
  67.  
  68.  /* function List() {
  69.     $("#tblList").html("");
  70.     $("#tblList").html(
  71.             "<thead>" +
  72.             "<tr>" +                
  73.             "<th>ID</th>" +
  74.             "<th>Nombre</th>" +
  75.             "<th>Teléfono</th>" +
  76.             "<th>Email</th>" +
  77.             "<th>Acciones</th>" +
  78.             "</tr>" +
  79.             "</thead>" +
  80.             "<tbody>" +
  81.             "</tbody>"
  82.             );
  83.     for (var i in tblPersons) {
  84.         var per = JSON.parse(tblPersons[i]);
  85.         $("#tblList tbody").append("<tr>" +                    
  86.                 "<td>" + per.ID + "</td>" +
  87.                 "<td>" + per.Name + "</td>" +
  88.                 "<td>" + per.Phone + "</td>" +
  89.                 "<td>" + per.Email + "</td>" +                    
  90.                 "<td><img src='edit.png' alt='Edit" + i + "' class='btnEdit'/>&nbsp &nbsp<img src='delete.png' alt='Delete" + i + "' class='btnDelete'/></td>" +
  91.                 "</tr>"
  92.                 );
  93.     }
  94.   }
  95. */
  96.     //if (operation === "C")
  97.         //return Create();
  98.     //else
  99.       //  return Edit();
  100.   //});
  101.  
  102.  // List();
  103.  
  104.  /* $(".btnEdit").bind("click", function () {
  105.     operation = "E"; //"E" = Editar
  106.     selected_index = parseInt($(this).attr("alt").replace("Edit", ""));
  107.     var per = JSON.parse(tblPersons[selected_index]);
  108.     $("#txtID").val(per.ID);
  109.     $("#txtName").val(per.Name);
  110.     $("#txtPhone").val(per.Phone);
  111.     $("#txtEmail").val(per.Email);
  112.     $("#txtID").attr("readonly", "readonly");
  113.     $("#txtName").focus();
  114.   });*/
  115.  
  116.  /* $(".btnDelete").bind("click", function () {
  117.     selected_index = parseInt($(this).attr("alt").replace("Delete", ""));
  118.     Delete();
  119.     List();
  120.   });*/
Add Comment
Please, Sign In to add comment