我正在尝试使用jQuery更新数据库。该功能工作正常,但只能运行一次。如果要再次更新数据库,我必须刷新页面并清除缓存。我正在使用MODX Revolution,不确定是否与该问题有关。这是代码:
<html>
<head>
<script language="javascript" type="text/javascript" src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
</head>
<body>
<script>
$(document).ready(function(){
$('#paina').submit( function updateTitle() {
var mail = $("#output").val();
$.ajax({
type: "POST",
url: "gettable.php",
data: "mail="+ mail,
cache: false,
success: function(data){
alert(mail);
}
});
return false;
});
});
</script>
<form id="paina" name="test" action="javascript:updateTitle()" method="post">
<input type="text" id="output">
<input type="Submit" value="Update">
</form>
</body>
</html>
PHP:
<?php
$mail=$_POST["mail"];
$mysqli = mysqli_connect("url", "user", "password", "database_name");
mysqli_query($mysqli, "UPDATE modx_site_content SET pagetitle='$mail' WHERE longtitle='jee'");
echo "Updated!";
$mysqli->close();
最佳答案
从jQuery文档中:
注意:将缓存设置为false只能与HEAD和
GET请求
难以解决:将随机参数附加到查询字符串:
$.ajax({
type: "POST",
url: "gettable.php",
data: "mail="+ mail + "×tamp=" + new Date(), // Will append the current date in milliseconds since January 1, 1970 (correct me if I'm wrong).
cache: false,
success: function(data){
alert(mail);
}
});