用ajax PHP的MySQL与HTML链接点击更新数据库

问题描述:

我已经经历了很多类似的问题,阅读并尽我的手在把它的工作在我的网站,但它不工作(当您单击的链接出现在控制台上没有任何反应,数据库未更新)。

I've read through a number of similar questions and tried my hand at putting it to work on my website, but it is not working (when you click the link there is no response on the console and the database is not updated).

这就是我想做的事:我希望用户通过单击旁边的注释图标评论评论 +1 。我想它来更新我的MySQL comment_table 称为柱评级等级+ 1 。当我做没有AJAX(即,只要设置表单动作为一个 PHP页面?ID = 10 ),它工作正常。我不能让AJAX来,虽然更新数据库。

Here's what I want to do: I want users to rate a comment +1 by clicking an icon next to the comment. I want that to update my mysql comment_table column called rating with rating+1. When I do it without AJAX (ie, just set the form action to a php page?id=10) it works fine. I can't get the AJAX to update the database though.

与评论我的主页:

<a href="javascript:void(0);" onClick="updateRating(1,<?php echo $thisperspective_row['id']; ?>)" alt="UPVOTE" id="upvote_<?php echo $thisperspective_row['id']; ?>"><span class="glyphicon glyphicon-chevron-up"></span></a>

下面这个链接中的JavaScript:

The javascript below that link:

<script type="text/javascript"> 
function updateRating(rating, id){

$.ajax({
    type: "GET",
    url: "rating.php",
    mode: "vote",
    rating: rating,
    id: <?php echo $thisperspective_row['id']; ?>,
    success: function(response) {
     console.log(response); 
    }
});
return false; // Prevent the browser from navigating to the-script.php
                };
</script>

和我的rating.php文件

and my rating.php file is

<?php
require_once('connectiontodatabase.php'); 

/* simple comment up and down voting script */
$mode = $_GET['mode'];
$rating = $_GET['rating'];
$id = $_GET['id'];

if ($mode=="vote")
    {
    // The name of the 
    $cookie = "nameofmycookie".$id;  
    if(isset($_COOKIE[$cookie])) 
        { 
        echo '<div class="alert alert-warning"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> Sorry You have already rated this comment within the last 14 days.</div>'; 
        } 
    else 
        {
        $expiry = 1209600 + time();  // 14 day expiry
        setcookie ($cookie, "voted", $expiry);
        mysql_query ("UPDATE comment_table SET rating = rating+$rating WHERE id=$id", $connection);
        }
    }

?>

在PHP运行正常,所有的变量都列出正确的,当我查看源。然而,当我点击链接,就完全没有反应,控制台不输出的响应。我究竟做错了什么?在此先感谢!

The php runs fine and all the variables are listed properly when I view the source. However, when I click the link, there is no response at all and the console does not output the response. What am I doing wrong? Thanks in advance!

首先,您应该更改所检测到的单击事件的方式。请查看这个小提琴。再其次,你需要通过传递所有的变量在一个 JSON字符串使用数据选项。您的code应该是这个样子:

Firstly you should change the way you are detecting the click event. Check out this fiddle. Then secondly you need to pass all the variables through in one JSON string using the data option. Your code should look something like this:

<span class="glyphicon glyphicon-chevron-up clickable" 
    data-rating="1" 
    data-id="<?php echo $thisperspective_row['id']; ?>"></span>

<script type="text/javascript">
    $('.clickable').on('click', function() {
        var data = {
            mode: "vote",
            rating: $(this).data('rating'),
            id: $(this).data('id')
        };
        $.ajax({
            type: 'GET',
            url: 'rating.php',
            data: data,
            success: function(response) {
                console.log(response);
            }
        });
    });
</script>