我正在用 phonegap 和 jQuery mobile 构建一个应用程序。在我的应用程序中,我有一个更新 mysql 的按钮,成功后定位到不同的页面。当我按下按钮 mysql 得到更新,但浏览器没有定位到不同的页面,所以我猜功能不成功?window.locate
这是我使用的代码,是否存在一些问题,或者即使更新部分可以,为什么也不能工作?
html:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<div id="detailsPage" data-role="page" data-add-back-btn="true">
<div data-role="header">
<h1>Header</h1>
</div>
<div data-role="content">
<input type="button" id="button_id" value="UPDATE" data-theme="b" onClick="UpdateRecord();">
<input type="button" id="button_id" value="DELETE="d" onClick="DeleteRecord();">
</div>
</body>
</html>
js:
<script>
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
function UpdateRecord(update_id) {
var id = getUrlVars()["id"];
jQuery.ajax({
type: "POST",
url: serviceURL + "update.php",
data: 'id='+id ,
cache: false,
success: function(response)
{
window.location = "list.html"
}
});
}
function DeleteRecord(update_id) {
var id = getUrlVars()["id"];
jQuery.ajax({
type: "POST",
url: serviceURL + "delete.php",
data: 'id='+id ,
cache: false,
success: function(response)
{
window.location = "list.html"
}
});
}
</script>
和update.php:
<?php
include 'config.php';
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data
$id = @$_POST['id'];
// query
$sql = "UPDATE table
SET number=3
WHERE IND=?";
$q = $conn->prepare($sql);
$q->execute(array($id));
?>