0

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

像.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'];
?>

索引.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>

查询:

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

4

1 回答 1

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>
于 2012-07-02T11:22:36.343 回答