Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 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 { Http, Headers, RequestOptions } from '@angular/http';
  6. import 'rxjs/add/operator/map';
  7. //import { PInfoPage } from '../p-info/p-info';
  8.  
  9. @IonicPage()
  10. @Component({
  11. selector: 'page-acc-info',
  12. templateUrl: 'acc-info.html',
  13. })
  14. export class AccInfoPage {
  15.  
  16. // Define FormBuilder /model properties
  17. public form : FormGroup;
  18. public AdminUsername : any;
  19. public PatientUsername : any;
  20. public AccountPassword : any;
  21. // Flag to hide the form upon successful completion of remote operation
  22. public isEdited : boolean = false;
  23. public hideForm : boolean = false;
  24. // Property to store the recordID for when an existing entry is being edited
  25. public AccountID : any = null;
  26. private baseURI : string = "http://localhost:10080/ionic/";
  27.  
  28. // Initialise module classes
  29. constructor(public navCtrl : NavController,
  30. public http : Http,
  31. public NP : NavParams,
  32. public fb : FormBuilder,
  33. public toastCtrl : ToastController)
  34. {
  35.  
  36. // Create form builder validation rules
  37. this.form = fb.group({
  38. "a_username" : ["", Validators.required],
  39. "p_username" : ["", Validators.required],
  40. "password" : ["", Validators.required],
  41. });
  42. }
  43.  
  44. ionViewWillEnter()
  45. {
  46. this.resetFields();
  47.  
  48. if(this.NP.get("account"))
  49. {
  50.  
  51. this.isEdited = false
  52. }
  53. }
  54.  
  55. // Assign the navigation retrieved data to properties
  56. // used as models on the page's HTML form
  57. selectEntry(item)
  58. {
  59. this.AdminUsername = item.a_username;
  60. this.PatientUsername = item.p_username;
  61. this.AccountPassword = item.password;
  62. this.AccountID = item.acc_id;
  63. }
  64.  
  65.  
  66. createEntry(a_username, p_username, password)
  67. {
  68. let body : string = "key=create&a_username=" + a_username +
  69. "&p_username=" + p_username + "&password=" + password,
  70. type : string = "application/x-www-form-urlencoded; charset=UTF-
  71. 8",
  72. headers : any = new Headers({ 'Content-Type':type}),
  73. options : any = new RequestOptions({ headers: headers }),
  74. url : any = this.baseURI + "manage-data.php";
  75.  
  76. this.http.post(url, body, options)
  77. .subscribe((data) =>
  78. {
  79. // If the request was successful notify the user
  80. if(data.status === 200)
  81. {
  82. this.hideForm = true;
  83. this.sendNotification(`Congratulations the account was successfully
  84. created`);
  85. }
  86. // Otherwise let 'em know anyway
  87. else
  88. {
  89. this.sendNotification('Something went wrong!');
  90. }
  91. });
  92. }
  93.  
  94. // Handle data submitted from the page's HTML form
  95. // Determine whether we are adding a new record or amending an
  96. // existing record
  97. saveEntry()
  98. {
  99. let a_username : string = this.form.controls["a_username"].value,
  100. p_username : string = this.form.controls["p_username"].value,
  101. password : string = this.form.controls["password"].value
  102.  
  103.  
  104. this.createEntry(a_username,p_username, password);
  105. //this.navCtrl.push(PInfoPage);
  106.  
  107. }
  108.  
  109. // Clear values in the page's HTML form fields
  110. resetFields() : void
  111. {
  112. this.AdminUsername = "";
  113. this.PatientUsername = "";
  114. this.AccountPassword = "";
  115. }
  116.  
  117. // Manage notifying the user of the outcome
  118. // of remote operations
  119. sendNotification(message) : void
  120. {
  121. let notification = this.toastCtrl.create({
  122. message : message,
  123. duration : 3000
  124. });
  125. notification.present();
  126. }
  127. }
  128.  
  129. <?php
  130.  
  131. header('Access-Control-Allow-Origin: *');
  132.  
  133. // Define database connection parameters
  134. $hn = 'localhost';
  135. $un = 'root';
  136. $pwd = '';
  137. $db = 'ring a bell';
  138. $cs = 'utf8';
  139.  
  140. // Set up the PDO parameters
  141. $dsn = "mysql:host=" . $hn . ";port=3306;dbname=" . $db . ";charset=" .
  142. $cs;
  143. $opt = array(
  144. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  145. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
  146. PDO::ATTR_EMULATE_PREPARES => false,
  147. );
  148. // Create a PDO instance (connect to the database)
  149. $pdo = new PDO($dsn, $un, $pwd, $opt);
  150.  
  151. // Retrieve specific parameter from supplied URL
  152. $key = strip_tags($_REQUEST['key']);
  153. $data = array();
  154. switch($key)
  155. {
  156. // Add a new record to the technologies table
  157. case "create":
  158.  
  159. // Sanitise URL supplied values
  160. $a_username = filter_var($_REQUEST['a_username'],
  161. FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
  162. $p_username = filter_var($_REQUEST['p_username '],
  163. FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
  164. $password = filter_var($_REQUEST['password'],
  165. FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
  166.  
  167. // Attempt to run PDO prepared statement
  168. try {
  169. $sql = "INSERT INTO account_info(a_username, p_username, password)
  170. VALUES(:a_username, :p_username, :password)";
  171. $stmt = $pdo->prepare($sql);
  172. $stmt->bindParam(':a_username', $a_username, PDO::PARAM_STR);
  173. $stmt->bindParam(':p_username', $p_username, PDO::PARAM_STR);
  174. $stmt->bindParam(':password', $password, PDO::PARAM_STR);
  175.  
  176. $stmt->execute();
  177.  
  178. echo json_encode(array('message' => 'Congratulations the Account
  179. was added to the database'));
  180. }
  181. // Catch any errors in running the prepared statement
  182. catch(PDOException $e)
  183. {
  184. echo $e->getMessage();
  185. }
  186.  
  187. break;
  188. }
  189. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement