2014-04-08 187 views
1

我已阅读了一些类似的问题,并试图将其放到我的网站上工作,但它不起作用(当您单击链接时没有控制台和数据库上的响应未更新)。用html链接更新数据库点击使用ajax php mysql

以下是我想要做的:我希望用户通过单击评论旁边的图标来评论评论+1。我想要更新我的mysql comment_tableratingrating+1。当我没有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运行正常,并当我查看源代码时,所有变量都已正确列出。但是,当我单击链接时,根本没有响应,并且控制台不输出响应。我究竟做错了什么?提前致谢!

+0

在阿贾克斯你需要发送的数据是这样,'数据:{模式:“投票”,等级:等级,编号:ID}' –

回答

2

首先你应该改变的方式你正在检测点击事件。退房this fiddle。然后,您需要使用数据选项在一个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> 
+0

谢谢@Styphon。当我对你的建议代码进行了一个改变时,你的解决方案就工作了: 而不是输入'$ .ajax(function(){'我输入'.ajax({' – GK79

+0

@ GK79是的,对于大多数jQuery,在那里,但不是为了ajax,我必须在习惯中加入它,并且更新我的答案,很高兴帮助。 – Styphon

0

首先关闭所有,请检查您是否加载了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 
}; 

为我工作。 注意,我删除了`里面阿贾克斯,因为你已经在函数发送参数,可以同时发送PARAMS你必须使用数据:,你可以在这里看到更多的例子Ajax jQuery

+0

感谢您的答复。我没有足够先进的jQuery来理解为什么链接的onClick事件不能触发该函数(检出@ Styphon的小提琴)。它不适合我,所以我决定采用Styphon的上述解决方案。感谢您的回复! – GK79

+0

谢谢,我还建议你将'mysql_real_escape_string()'添加到你从用户那里获得的任何** GET **或** POST **中。 你可以在这里看到更多关于该功能的信息。 [链接](http://php.net/mysql_real_escape_string) – robzdc