我假设我必须在成功选项中添加一些内容。但是我所拥有的不起作用。
我在页面声明了这个JS函数:
<script type="text/javascript">
function performAjaxSubmission() {
$.ajax({
url: 'addvotetotable.php',
method: 'POST',
data: {
},
success: function() {
}
});
}
</script>
然后在正常工作的ajax调用中我声明此成功:
success: function(data) {
performAjaxSubmission();
},
addvotetotable.php 看起来像:
<
?php
// If user submitted vote give the user points and increment points counter
require_once("models/config.php");
//Check to see if there is a logged in user
if(isUserLoggedIn()) {
$username_loggedin = $loggedInUser->display_username;
}
if (strlen($username_loggedin)) {
include_once "scripts/connect_to_mysql.php";
$query = mysql_query("SELECT vote FROM points_settings WHERE id=1")or die (mysql_error());
while($info = mysql_fetch_array($query)){
$points_value=$info['vote'];
}
include_once "scripts/connect_to_mysql.php";
$query = mysql_query("INSERT INTO points (id,username,action,points) VALUES ('','$username_loggedin','vote','$points_value')")or die (mysql_error
());
include_once "scripts/connect_to_mysql.php";
$query = mysql_query("UPDATE userCake_Users SET points=points + $points_value WHERE Username='$username_loggedin'")or die (mysql_error());
}
?>
最佳答案
您必须在 ajax 请求的“数据”中提供要存储在数据库中的值。您可以在 php 代码中使用 $_POST["something"] 使用它,检查它是否有效...并返回 f.e.您可以在成功函数中处理 html 或 json。
<script type="text/javascript">
function onSubmitVote()
{
$.ajax({
url: 'addvotetotable.php',
method: 'POST',
data: "theVoteValue=" + <read_your_vote_value_here>,
success: function(result) {
>>>return something in your addvotetotable.php which indicate success and show a message whether the vote was successful or not.<<<
}
});
return false; // prevent submit if it is a submit-type button.
}
</script>
它的作用是将投票数据(它的值)发布到 php 文件。您可以使用 $_POST["theVoteValue"]
读取它并将其放入数据库中。您检查该值是否有效并且可以插入,否则您将返回错误消息或其他内容,以便在 JavaScript 中通知用户失败。
关于php - 使用 jQuery .ajax 作为投票脚本。投票成功后如何更新MySQL DB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7511047/