Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hi
- Here is a simple method using PHP but can easily be used with an ASP.NET web service.
- 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:-
- function getUser() {
- $("#myuser").empty();
- var myuser = $("#username").val();
- var mypassword = $("#password").val();
- var sqlstr = "Your URL/getUser.php?user="+myuser;
- var xhr = new XMLHttpRequest();
- xhr.open("GET",sqlstr,false);
- xhr.onload = function(){
- if(xhr.status==200)
- {
- jsonArray = $.parseJSON(xhr.responseText);
- var html = "<table width='100%' border='0'><tbody>"
- 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'>";
- for(i=0; i < jsonArray.length; i++)
- {
- user = jsonArray[i].username;
- pass = jsonArray[i].password;
- localStorage.setItem("myusername", user);
- localStorage.setItem("my password", pass);
- html += "<tr><td><input value='"+user+"'></td><td><input value='"+pass+"'></td></tr>";
- }
- html += "</tbody></table>";
- $(html).appendTo("#myuser");
- }
- else if(xhr.status == 404)
- {
- navigator.notification.alert("Web Service Doesn't Exist", "Error","OK");
- }
- else
- {
- navigator.notification.alert("Unknown error occured while connecting to server", "Error","OK");
- }
- }
- }
- xhr.send()
- }
- 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.
- <?php
- $User = $_GET['user'];
- $DB_NAME = "*****";
- $DB_USER = "******";
- $DB_PASSWORD = "******";
- $DB_HOST = "******";
- $db_handle = mysql_connect($DB_HOST,$DB_USER,$DB_PASSWORD)
- or die("Unable to connect to MySQL");
- $db_found = mysql_select_db($DB_NAME, $db_handle);
- $SQL = "SELECT username, password FROM users WHERE username= $User";
- $result = mysql_query($SQL);
- $rows = array();
- while($r = mysql_fetch_assoc($result)) {
- $rows[] = $r;
- }
- echo json_encode($rows);
- Hope that helps. Apologies for any coding errors, this may not run as written.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement