Titanium Save info to DB


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //var win = Titanium.UI.currentWindow;
  2. var currentWin = Ti.UI.currentWindow;
  3.  
  4. // if successful login - save user data to database
  5. function insertRows(dbData) {
  6. // call the database and table
  7. var db = Ti.Database.install('../myapp.sqlite','users');
  8. // call out the info to save then save it
  9. var theData = db.execute('INSERT INTO users (username, email)
  10. VALUES("'+dbData.name+'","'+dbData.email+'")');
  11.  
  12. };
  13.  
  14. // run the db.execute line
  15. theData;
  16. // alert on success
  17. alert("Rows Inserted");
  18.  
  19. };
  20.  
  21. var username = Titanium.UI.createTextField({
  22. color:'#336699',
  23. top:10,
  24. left:10,
  25. width:300,
  26. height:40,
  27. hintText:'Username',
  28. keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
  29. returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
  30. borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
  31. });
  32. currentWin.add(username);
  33.  
  34. var password = Titanium.UI.createTextField({
  35. color:'#336699',
  36. top:60,
  37. left:10,
  38. width:300,
  39. height:40,
  40. hintText:'Password',
  41. passwordMask:true,
  42. keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
  43. returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
  44. borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
  45. });
  46. currentWin.add(password);
  47.  
  48. var loginBtn = Titanium.UI.createButton({
  49. title:'Login',
  50. top:110,
  51. width:90,
  52. height:35,
  53. borderRadius:1,
  54. font:{fontFamily:'Arial',fontWeight:'bold',fontSize:14}
  55. });
  56. currentWin.add(loginBtn);
  57.  
  58. var loginReq = Titanium.Network.createHTTPClient();
  59.  
  60. loginReq.onload = function()
  61. {
  62. var json = this.responseText;
  63. var response = JSON.parse(json);
  64.  
  65. // if we get a positive response from the server
  66. if (response.logged == true) {
  67. // set the variables for our user db table
  68. var dbData = {
  69. name:response.name,
  70. email:response.email
  71.  
  72. };
  73. // add the above var data to the table
  74. insertRows(dbData);
  75.  
  76. // fire this alert as proof of what we are being returned
  77. //alert( response.name + response.email);
  78.  
  79. username.blur();
  80. password.blur();
  81. Ti.App.fireEvent('grantEntrance', {
  82. name:response.name,
  83. email:response.email
  84.  
  85. });
  86. //win.close();
  87. }
  88. // if the username pw combo returns false
  89. else
  90. {
  91. // alert(response.message + response.username + response.password);
  92. alert(response.message);
  93. }
  94. };
  95.  
  96. loginBtn.addEventListener('click',function(e)
  97. {
  98. if (username.value != '' && password.value != '')
  99. {
  100. // where are we sending the login credentials
  101. loginReq.open("POST","http://mysite.com/users/app_login");
  102. // what are we sending to the server
  103. var params = {
  104. username: username.value,
  105. // hashed value
  106. //password: Ti.Utils.md5HexDigest(password.value)
  107. // unhashed value
  108. password: password.value
  109. };
  110. loginReq.send(params);
  111. }
  112. else
  113. {
  114. alert("Username/Password are required");
  115. }
  116. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.