Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import { Component } from '@angular/core';
  2. import { IonicPage, NavController, NavParams, ToastController } from 'ionic-
  3. angular';
  4. import { FormGroup, Validators, FormBuilder } from '@angular/forms';
  5. import { HttpModule, HttpClientModule, Headers, RequestOptions } from
  6. '@angular/http';
  7. import 'rxjs/add/operator/map';
  8. import { PInfoPage } from '../p-info/p-info';
  9.  
  10. @IonicPage()
  11. @Component({
  12. selector: 'page-acc-info',
  13. templateUrl: 'acc-info.html',
  14. })
  15. export class AccInfoPage {
  16.  
  17. // Define FormBuilder /model properties
  18. public form : FormGroup;
  19. public a_username : any;
  20. public p_username : any;
  21. public password : any;
  22. // Flag to hide the form upon successful completion of remote operation
  23. public hideForm : boolean = false;
  24. // Property to store the recordID for when an existing entry is being edited
  25. public acc_id : any = null;
  26. private baseURI : string = "http://localhost:10080/ionic/";
  27.  
  28.  
  29.  
  30. // Initialise module classes
  31. constructor(public navCtrl : NavController,
  32. public http : Http,
  33. public NP : NavParams,
  34. public fb : FormBuilder,
  35. public toastCtrl : ToastController)
  36. {
  37.  
  38. // Create form builder validation rules
  39. this.form = fb.group({
  40. "a_username" : ["", Validators.required],
  41. "p_username" : ["", Validators.required],
  42. "password" : ["", Validators.required],
  43. });
  44. }
  45.  
  46.  
  47.  
  48. // Assign the navigation retrieved data to properties
  49. // used as models on the page's HTML form
  50. selectEntry(item)
  51. {
  52. this.a_username = item.a_username;
  53. this.p_username = item.p_username;
  54. this.password = item.password;
  55. this.acc_id = item.acc_id;
  56. }
  57.  
  58.  
  59. createEntry(a_username, p_username, password)
  60. {
  61. let body : string = "key=create&a_username=" + a_username +
  62. "&p_username=" + p_username + "&password=" + password,
  63. type : string = "application/x-www-form-urlencoded; charset=UTF-
  64. 8",
  65. headers : any = new Headers({ 'Content-Type':application/json}),
  66. options : any = new RequestOptions({ headers: headers }),
  67. url : any = this.baseURI + "manage-data.php";
  68.  
  69. this.http.post(url, body, options)
  70. .subscribe((data) =>
  71. {
  72. // If the request was successful notify the user
  73. if(data.status === 200)
  74. {
  75. this.hideForm = true;
  76. this.sendNotification(`Congratulations the account was successfully
  77. created`);
  78. }
  79. // Otherwise let 'em know anyway
  80. else
  81. {
  82. this.sendNotification('Something went wrong!');
  83. }
  84. });
  85. }
  86.  
  87. // Handle data submitted from the page's HTML form
  88. // Determine whether we are adding a new record or amending an
  89. // existing record
  90. saveEntry()
  91. {
  92. let a_username : string = this.form.controls["a_username"].value,
  93. p_username : string = this.form.controls["p_username"].value,
  94. password : string = this.form.controls["password"].value
  95.  
  96.  
  97. this.createEntry(a_username,p_username, password);
  98.  
  99. }
  100.  
  101. // Clear values in the page's HTML form fields
  102. resetFields() : void
  103. {
  104. this.a_username = "";
  105. this.p_username = "";
  106. this.password = "";
  107. }
  108.  
  109.  
  110.  
  111. // Manage notifying the user of the outcome
  112. // of remote operations
  113. sendNotification(message) : void
  114. {
  115. let notification = this.toastCtrl.create({
  116. message : message,
  117. duration : 3000
  118. });
  119. notification.present();
  120. }
  121.  
  122.  
  123. open_p_info() {
  124. this.navCtrl.push(PInfoPage);
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement