gpt4 book ai didi

php - 将数据库添加到 jquery 移动站点

转载 作者:搜寻专家 更新时间:2023-10-30 20:05:58 24 4
gpt4 key购买 nike

我对使用 jquery 和 jquery mobile 还很陌生,但我想知道是否有一种简单的方法(最好是教程)将数据库(远程和本地)绑定(bind)到 jquery 移动站点(最好不使用 php) .

最佳答案

您可以通过 JavaScript 使用 HTML5 localStorage,这里有一个解释和一些教程链接:http://www.html5rocks.com/en/features/storage

...there are now several technologies allowing the app to save data on the client device...

如果您想与服务器交互,则需要使用服务器端脚本语言。使用 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

关于php - 将数据库添加到 jquery 移动站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8246380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com