Advertisement
Guest User

jsadrian

a guest
Apr 4th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function manageUser(operation) {
  2.  
  3.     var xmlhttp = new XMLHttpRequest();
  4.     var url = 'http://folk.ntnu.no/adrianto/prosjektesen/pushToDB.php/';
  5.    
  6.     var user_id;
  7.     var user_name;
  8.     var email;
  9.     var password;
  10.     var current_location_id;
  11.     var geomessage;
  12.  
  13.     xmlhttp.open('POST', url, true);
  14.  
  15.     xmlhttp.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
  16.  
  17.     xmlhttp.onreadystatechange = function()
  18.     {
  19.       if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
  20.       {
  21.         try{
  22.             switch(operation) {
  23.                 case 'newUser':
  24.                     var jsonData = JSON.parse(xmlhttp.responseText);
  25.                     user_id = jsonData.currval;
  26.                     alert('user_id = ' + user_id);
  27.                      break;
  28.                 case 'getUser':
  29.                     var jsonData = JSON.parse(xmlhttp.responseText);
  30.                     alert(jsonData);
  31.                     user_id = jsonData.user_id;
  32.                     current_location_id = jsonData.current_location_id;
  33.                     geomessage = jsonData.geomessage;  
  34.                     alert('user_id = ' + user_id);                    
  35.                     break;
  36.                 default:
  37.                     var success = xmlhttp.responseText == "true" ? true : false;
  38.                     alert(success);
  39.                     break;
  40.             }
  41.         } catch(e) {
  42.             alert('Kunne ikke evaluere svaret fra DB' + xmlhttp.responseText);
  43.  
  44.         }
  45.       }
  46.     }
  47.    
  48.     switch(operation) {
  49.         case 'newUser':
  50.             user_name = document.getElementById("user_name").value;
  51.             email = document.getElementById("email").value;
  52.             password = document.getElementById("pwd").value;
  53.             var user = {
  54.                 'sqlopt': 'insert',
  55.                 'table': 'public.user',
  56.                 'values': [
  57.                     {
  58.                         'column': 'user_name',
  59.                         'data': user_name
  60.                     },
  61.                     {
  62.                         'column': 'email',
  63.                         'data': email
  64.                     },
  65.                     {
  66.                         'column': 'password',
  67.                         'data': password
  68.                     },
  69.                 ]
  70.             };
  71.             break;
  72.         case 'getUser':
  73.             user_name = document.getElementById("user_name").value;
  74.             password = document.getElementById("pwd").value;
  75.             var user = {
  76.                 'sqlopt': 'select',
  77.                 'table': 'public.user',
  78.                 'to_select': [
  79.                     {
  80.                         'column': 'user_id'
  81.                     },
  82.                     {
  83.                         'column': 'current_location_id'
  84.                     },
  85.                     {
  86.                         'column': 'geomessage',
  87.                     }
  88.                 ],
  89.                 'conditions': [
  90.                     {
  91.                         'column': 'user_name',
  92.                         'data': user_name
  93.                     },
  94.                     {
  95.                         'column': 'password',
  96.                         'data': password
  97.                     },
  98.                 ]
  99.             };
  100.             break;
  101.         case 'deleteUser':
  102.             var user = {
  103.                 'sqlopt': 'delete',
  104.                 'table': 'public.user',
  105.                 'conditions': [
  106.                     {
  107.                         'column': 'user_id',
  108.                         'data': user_id
  109.                     }
  110.                 ]
  111.             };
  112.             break;
  113.         case 'updateUserLocation':
  114.             var user = {
  115.                 'sqlopt': 'update',
  116.                 'table': 'public.user',
  117.                 'values': [
  118.                     {
  119.                         'column': 'current_location_id',
  120.                         'data': current_location_id
  121.                     }
  122.                 ],
  123.                 'conditions': [
  124.                     {
  125.                         'column': 'user_id',
  126.                         'data': user_id
  127.                     }
  128.                 ]
  129.             };
  130.             break;
  131.         case 'updateUserGeomessage':
  132.             var user = {
  133.                 'sqlopt': 'update',
  134.                 'table': 'public.user',
  135.                 'values': [
  136.                     {
  137.                         'column': 'geomessage',
  138.                         'data': geomessage
  139.                     }
  140.                 ],
  141.                 'conditions': [
  142.                     {
  143.                         'column': 'user_id',
  144.                         'data': user_id
  145.                     }
  146.                 ]
  147.             };
  148.             break;
  149.     }
  150.     var usertext = JSON.stringify(user);
  151.     xmlhttp.send(usertext);
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement