JQuery问题与开发“喜欢”系统

时间:2012-06-28 06:27:48

标签: php jquery mysql wordpress

我正在尝试为我的网站开发一个“喜欢”的系统。我正在使用以下代码:

like.php:

<?php
include_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
global $wpdb;
$global_table = $wpdb->base_prefix . "global_products_table";
$blogid = $_GET['blog_id'];
$productsid = $_GET['products_id'];

mysql_query("UPDATE ".$global_table." SET likes=likes+1 WHERE blog_id=".$blogid." and products_id=".$productsid) or die(mysql_error());
$result = mysql_query("SELECT likes from ".$global_table." WHERE blog_id=".$blogid." and products_id=".$productsid) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo $row['likes'];
?>

的index.html:

<form name="like_form">
  <input type="hidden" name="products_id" value="<?php echo $product_info['products_id']; ?>" />
  <a class="like_click"><span class="button"><img src="http://cdn1.iconfinder.com/data/icons/bijou/10/Like.png"> Like (<span class="like_count"><?php echo $product_info['likes']; ?></span>)</span></a>
</form>

JQuery的:

<script type="text/javascript">
var $s = jQuery.noConflict();
$s(document).ready(function() {
  $s('.like_click').click(function() {
    var form = $s(this).closest('form[name=like_form]');
    var blog_id = $s(form).find('input[name=blog_id]').val();
    var products_id = $s(form).find('input[name=products_id]').val();
    $s.post("<?php echo get_bloginfo('wpurl'); ?>/wp-content/plugins/wp-online-store/like.php?blog_id=<?php echo get_current_blog_id(); ?>&products_id='" + products_id + ", function(data) {
      $s(form).find('span.like_count').html(data);
    });
});
</script>

问题:当我点击“赞”链接时,它无效。类似的计数不会更新。当我在地址栏中输入Url时,例如http://website.com/wp-content/plugins/wp-online-store/like.php?blog_id=16&products_id=37,它确实有效。这让我觉得问题出在JQuery上。另外,我正在使用Wordpress。

1 个答案:

答案 0 :(得分:0)

先试试

<script type="text/javascript">

var $s = jQuery.noConflict();
$s(document).ready(function() {
$s('.like_click').click(function() {
    var id=$(this).attr('rel');

$.post("http://website.com/wp-content/plugins/wp-online-store/like.php?blog_id=16&products_id="+id, 
   function(data) {
     $s(form).find('span.like_count').html(data);
   });
});
   });
</script>

<form name="like_form">
  <a rel="<?php echo $product_info['products_id']; ?>" class="like_click"><span class="button"><img src="http://cdn1.iconfinder.com/data/icons/bijou/10/Like.png"> Like (<span class="like_count"><?php echo $product_info['likes']; ?></span>)</span></a>
</form>