使用AJAX更新的值不会被添加到服务器数据库?服务器、数据库、AJAX

2023-09-10 22:03:31 作者:别让梦枯萎

我用这code调用ajaxvote.php

I am using this code to call ajaxvote.php

$('.vote').click(function(){
$.ajax({
url: 'ajaxvote.php',
type: 'POST',
cache: 'false',
success: function () { alert("Success!"); } ,
error: function () { alert("Error!"); }});
var self = $(this);
var action = self.data('action');
var parent = self.parent().parent();
var postid = parent.data('postid');
var score = parent.data('score');
if (!parent.hasClass('.disabled')) {
if (action == 'up') {
parent.find('.vote-score').html(++score).css({'color':'orange'});
self.css({'color':'orange'});
$.ajax({data: {'postid' : postid, 'action' : 'up'}});
}
else if (action == 'down'){
parent.find('.vote-score').html(--score).css({'color':'red'});
self.css({'color':'red'});
$.ajax({data: {'postid' : postid, 'action' : 'down'}});
};
parent.addClass('.disabled');

这是在code,从我的网页

This is the code from my webpage

<div class="item" data-postid="<?php echo $rows['ID'] ?>" data-score="<?php echo $rows['VOTE'] ?>">
<div class="vote-span">
<div class="vote" data-action="up" title="Vote up"><i class="fa fa-camera-retro"></i></div>
<div class="vote-score"><?php echo $rows['VOTE'] ?></div>
<div class="vote" data-action="down" title="Vote down"><i class="fa fa-camera-retro"></i></div>
</div>

这是我的PHP code

This is my php code

if ($_SERVER['HTTP_X_REQUESTED_WITH']) {
  if (isset($_POST['postid']) && (isset($_POST['action']))) {
    $postId = $_POST['postid'];
    if (isset($_SESSION['vote'][$postId])) return;
    $query = $mysqli - > query("SELECT VOTE from stories WHERE ID = $postId LIMIT 1");
    while ($rows = mysqli_fetch_array($query)) {
        if ($_POST['action'] === 'up') {
            $vote = ++$rows['VOTE'];
        } else {
            $vote = --$rows['VOTE'];
        }
        $mysqli - > query("UPDATE stories SET VOTE = $vote WHERE ID = $postId ");
        $_SESSION['vote'][$postId] = true;
    }
  }
}

我知道我可以连接到数据库,因为我可以登录。我也得到我已经建立了上述警告的成功,但是,这些值不更新的数据库。

I know I can connect to database because I can login. I also get the alert success I have set up above, However, the values are not updating in Database.

修改 我加,我已经写了阿贾克斯code。

EDIT I have added more Ajax code that I had already written.

推荐答案

在通过AJAX发布,则需要通过发送你真正想要发布的数据。

When posting via ajax, you need to send through the data you actually want to post.

var postData = {name:"Mister",lastName:"Frodo"}; //Array

$.ajax({
    url : "ajaxvote.php",
    type: "POST",
    data : postData,
    success: function(data, textStatus, jqXHR)
    {
        //Handle response
    },
    error: function (e) {
       // Handle error
    }
});

在这种情况下,后期ID和得分必须抓住了。您还需要抓住被点击什么样的行动(通常是通过在的div类=投票click事件绑定例如目的,我们只是将其设置为向上现在:

In this case, the post ID and score needs to be grabbed. You also need to grab what kind of action is clicked (typically through a click event bind on the divs with class="vote". For example purposes, let's just set it to "up" for now:

var postId = $('div.item').attr('data-postid').val();
var score = $('div.item').attr('data-score').val();
var postData = {postId: postId, score: score, action: 'up'}

现在,您可以张贴POSTDATA你ajaxvote.php。

You can now post that "postData" to your ajaxvote.php.

此外,您还可以使用jQuery US $ .POST方法

Also, you can use jQuery's $.POST method

$后(ajaxvote.php,{名称:先生,名字:佛罗多});

$.post("ajaxvote.php", { name: "Mister", lastName: "Frodo" } );

现在解析表单,看看 jQuery的序列化它通过你的表单需要每个输入的[名]随着价值属性来创建一个数据串。

Now for parsing your form, have a look at jQuery's serialize which goes through your form takes each input's [name] attribute along with the value to create a data-string.

示例

name=Mister&lastName=Frodo

这是理想的数据发送至属性在$阿贾克斯。 Have看看这个答案更多关于jQuery的序列化。

This is ideal for sending through with the "data" attribute in $.ajax. Have a look at this answer for more regarding jQuery's serialize.