【问题标题】:Update database with html link click using ajax php mysql使用 ajax php mysql 使用 html 链接单击更新数据库
【发布时间】:2014-05-20 13:11:46
【问题描述】:

我已经阅读了许多类似的问题,并尝试在我的网站上使用它,但它不起作用(当您单击链接时,控制台上没有响应并且数据库没有更新)。

这就是我想要做的:我希望用户通过点击评论旁边的图标来评价评论+1。我希望它用rating+1 更新我的名为rating 的mysql comment_table 列。当我在没有 AJAX 的情况下执行此操作时(即,只需将表单操作设置为 php page?id=10),它工作正常。我无法让 AJAX 更新数据库。

我的主页有评论:

<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:

<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 文件是

<?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 运行良好,所有变量都正确列出。但是,当我单击链接时,根本没有响应,控制台也没有输出响应。我究竟做错了什么?提前致谢!

【问题讨论】:

  • 在 Ajax 中你需要像这样发送数据,data: {mode: "vote", rating: rating, id: id}

标签: javascript php jquery mysql ajax


【解决方案1】:

首先,检查您是否正在加载 jQuery,然后使用此代码

function updateRating(rating, id){
  $.ajax({
    type: "GET",
    url: "rating.php",
    mode: "vote",
    data: { rating: rating, id: id },
    success: function(response) {
     console.log(response); 
    }
  });
return false; // Prevent the browser from navigating to the-script.php
};

为我工作。 请注意,我在 Ajax 中删除了 `,因为您已经在函数中发送参数,所以要发送参数,您必须使用 data:,您可以在此处查看更多示例 Ajax jQuery

【讨论】:

  • 感谢您的回复。我在 jquery 中不够先进,无法理解为什么链接的 onClick 事件无法触发该功能(查看@Styphon 的小提琴)。它对我不起作用,所以我决定采用上述 Styphon 的解决方案。不过感谢您的回复!
  • 谢谢,我还建议您将mysql_real_escape_string() 添加到您从用户那里获得的任何 GET 或 POST 中。您可以在此处查看有关该功能的更多信息。 link
【解决方案2】:

首先,您应该更改检测点击事件的方式。查看this fiddle。其次,您需要使用 data 选项在一个 JSON string 中传递所有变量。您的代码应如下所示:

<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>

【讨论】:

  • 谢谢@Styphon。当我对您建议的代码进行一项更改时,您的解决方案有效:我没有输入$.ajax(function() {,而是输入了$.ajax({
  • @GK79 是的,对于大多数 jQuery,你有 function() 在那里,但不是 ajax。我一定是出于习惯添加了它,呵呵。更新了我的答案。很高兴它有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 2017-08-22
  • 1970-01-01
相关资源
最近更新 更多