我对使用jquery和jquery mobile很陌生,但我想知道是否有一种简单的方法(或教程最好)将数据库(远程和本地)绑定到jquery移动站点(最好不使用php)。
你可以通过JavaScript使用HTML5 localStorage,这里有一个解释和一些教程链接:http://www.html5rocks.com/en/features/storage
…现在有几种技术允许应用程序保存数据客户端设备…
如果你想与服务器交互,那么你需要使用服务器端脚本语言。使用AJAX与服务器通信非常简单:
JS -
//run the following code whenever a new pseudo-page is created
$(document).delegate('[data-role="page"]', 'pagecreate', function () {
//cache this page for later use (inside the AJAX function)
var $this = $(this);
//make an AJAX call to your PHP script
$.getJSON('path_to/server/script.php', function (response) {
//create a variable to hold the parsed output from the server
var output = [];
//if the PHP script returned a success
if (response.status == 'success') {
//iterate through the response rows
for (var key in response.items) {
//add each response row to the output variable
output.push('<li>' + response.items[key] + '</li>');
}
//if the PHP script returned an error
} else {
//output an error message
output.push('<li>No Data Found</li>');
}
//append the output to the `data-role="content"` div on this page as a listview and trigger the `create` event on its parent to style the listview
$this.children('[data-role="content"]').append('<ul data-role="listview">' + output.join('') + '</ul>').trigger('create');
});
});
PHP——<?php
//include your database connection code
include_once('database-connection.php');
//query your MySQL server for whatever information you want
$query = mysql_query("SELCT * FROM fake_table WHERE fake_col='fake value'", $db) or trigger_error(mysql_error());
//create an output array
$output = array();
//if the MySQL query returned any results
if (mysql_affected_rows() > 0) {
//iterate through the results of your query
while ($row = mysql_fetch_assoc($query)) {
//add the results of your query to the output variable
$output[] = $row;
}
//send your output to the browser encoded in the JSON format
echo json_encode(array('status' => 'success', 'items' => $output));
} else {
//if no records were found in the database then output an error message encoded in the JSON format
echo json_encode(array('status' => 'error', 'items' => $output));
}
?>
还可以将数据发送到PHP脚本,并将其添加到数据库中。
下面是上面使用的函数的一些文档页:
jQuery .getJSON()
: http://api.jquery.com/jquery.getjson
PHP json_encode()
: http://www.php.net/json_encode
use http://westcoastlogic.com/lawnchair/
它提供了许多存储方式,您实际上可以将所有适配器(如何存储的选项)按顺序排列,以便它将选择浏览器上可用的第一个适配器。此外,无论你想使用本地存储还是sqlite,它都使用JSON格式,所以你只需要处理JSON数据。
如果你想要一个数据库,在移动浏览器中有以Web SQL数据库的形式对SQLLite的广泛支持,目前大多数android和iPhone设备都支持。
查看工作示例,请查看以下链接:
http://desalasworks.com/html5-databases-on-iphone/请注意,在SQLLite中可用的SQL语言比(我认为是)您的MySQL数据库更有限。您可以创建/删除表、选择、插入和更新,但是缺少一些更高级的操作。