Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. Hi
  2.  
  3. Here is a simple method using PHP but can easily be used with an ASP.NET web service.
  4.  
  5. In XDK create a javascript function like this, I take a value from some inputs with the ID username and password, note I also store this info in localStorage items for later use. This is how I preserve data that also persists after an App restart. The returned result is displayed in HTML. This line of code writes the html to a DIV with the ID myuser. $(html).appendTo("#myuser"). Note how I parse the JSON data from the jsonArray with each column name from the selection in PHP:-
  6.  
  7. function getUser() {
  8. $("#myuser").empty();
  9. var myuser = $("#username").val();
  10. var mypassword = $("#password").val();
  11.  
  12. var sqlstr = "Your URL/getUser.php?user="+myuser;
  13.  
  14. var xhr = new XMLHttpRequest();
  15.  
  16. xhr.open("GET",sqlstr,false);
  17.  
  18. xhr.onload = function(){
  19.  
  20. if(xhr.status==200)
  21.  
  22. {
  23.  
  24. jsonArray = $.parseJSON(xhr.responseText);
  25.  
  26. var html = "<table width='100%' border='0'><tbody>"
  27.  
  28. html += "<tr><td width='120px' align='right' valign='middle'>Your Details</td><td align='left' valign='middle'><div class='table-thing with-label widget uib_w_60 d-margins SelectWidth' data-uib='app_framework/select' data-ver='1'>";
  29.  
  30. for(i=0; i < jsonArray.length; i++)
  31.  
  32. {
  33.  
  34. user = jsonArray[i].username;
  35.  
  36. pass = jsonArray[i].password;
  37.  
  38. localStorage.setItem("myusername", user);
  39.  
  40. localStorage.setItem("my password", pass);
  41.  
  42. html += "<tr><td><input value='"+user+"'></td><td><input value='"+pass+"'></td></tr>";
  43.  
  44. }
  45.  
  46. html += "</tbody></table>";
  47.  
  48. $(html).appendTo("#myuser");
  49.  
  50. }
  51.  
  52. else if(xhr.status == 404)
  53.  
  54. {
  55.  
  56. navigator.notification.alert("Web Service Doesn't Exist", "Error","OK");
  57.  
  58. }
  59.  
  60. else
  61.  
  62. {
  63.  
  64. navigator.notification.alert("Unknown error occured while connecting to server", "Error","OK");
  65.  
  66. }
  67.  
  68. }
  69.  
  70. }
  71.  
  72. xhr.send()
  73.  
  74. }
  75.  
  76.  
  77. You will see that xhr calls the PHP page getUser.php?user="+myuser that returns the result as JSON data. I have hidden my SQL server settings in the code below.
  78.  
  79.  
  80.  
  81. <?php
  82.  
  83. $User = $_GET['user'];
  84.  
  85. $DB_NAME = "*****";
  86.  
  87. $DB_USER = "******";
  88.  
  89. $DB_PASSWORD = "******";
  90.  
  91. $DB_HOST = "******";
  92.  
  93. $db_handle = mysql_connect($DB_HOST,$DB_USER,$DB_PASSWORD)
  94.  
  95. or die("Unable to connect to MySQL");
  96.  
  97. $db_found = mysql_select_db($DB_NAME, $db_handle);
  98.  
  99. $SQL = "SELECT username, password FROM users WHERE username= $User";
  100.  
  101. $result = mysql_query($SQL);
  102.  
  103. $rows = array();
  104.  
  105. while($r = mysql_fetch_assoc($result)) {
  106.  
  107. $rows[] = $r;
  108.  
  109. }
  110.  
  111. echo json_encode($rows);
  112.  
  113. Hope that helps. Apologies for any coding errors, this may not run as written.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement