-1

I am trying to create a love it button the button is here www.dreamsynk.com.

I have taken code from different places and pasted them together, but there are things that are wrong i just am not sure what as i don't know PHP or ajax and my jquery is knowledge is minimal.

I have a database and a table called "loveit" with a field called "value".

Here is my code so far:

HTML:

<div class="love-it">
    <script type="text/javascript">
    jQuery(function() {
        jQuery('#like-it').click(function() {
        jQuery('#like-it').load('wp-content/themes/dreamsynk/value.php');
        });
    });​
    </script>
    <a href="#" class="btn btn-counter" id="like-it" data-count="0"><span>❤&lt;/span></a>
</div>

script.js

jQuery(document).ready(function() {

(function($) {
$('.btn-counter').on('click', function(event, count) {
event.preventDefault();

var $this = $(this),
    count = $this.attr('data-count'),
    active = $this.hasClass('active'),
    multiple = $this.hasClass('multiple-count');

// First method, allows to add custom function
// Use when you want to do an ajax request
if (multiple) {
    $this.attr('data-count', ++count);
    $("#like-it").unbind().bind("click", function() {
        $.ajax({
        type: 'POST',
        url: 'wp-content/themes/dreamsynk/loveit.php',
        data: 'action=add',
        success: function(result) {
        $("#like-it").html(result);
        }
        });
        return false;
    });
} else {
    $this.attr('data-count', active ? --count : ++count).toggleClass('active');
    $("#like-it").unbind().bind("click", function() {
        $.ajax({
        type: 'POST',
        url: 'wp-content/themes/dreamsynk/loveit.php',
        data: 'action=add',
        success: function(result) {
        $("#like-it").html(result);
        }
        });
        return false;
    });
} 
})
})(jQuery);

loveit.php

<?php mysql_connect("host", "database", "pass") or die    ("Error.");
mysql_select_db("database") or die ("error");

$increase = "UPDATE loveit SET value=value+1 WHERE id=1;";
$active_rate = mysql_query("SELECT * FROM loveit WHERE id=1;");
$val = 0;

if($rt = mysql_fetch_assoc($active_rate)) {
$val = $rt['value'];
}

if($_POST['action'] == 'add') {
mysql_query($increase);
print $val++;
}

$rat = mysql_query("SELECT * FROM loveit WHERE id=1;");

if($res = mysql_fetch_assoc($rat)) {
print '<a id="likeit'.($res['value']-1).' '; // id="likeit" 
}   
?>

value.php

<?php mysql_connect("host", "database", "pass") or die ("Error.");
mysql_select_db("database") or die ("error");

$rat = mysql_query("SELECT * FROM loveit WHERE id=1;");

if($res = mysql_fetch_assoc($rat)) {
    print ($res['value']-1); 
}

?>

And then obviously my stylesheet.

What have I done wrong here?

The button needs to be limited to one click like it is now and if they click it again it unloves me.

4

2 回答 2

1

This may not fully answer all your questions, but will indicate some of the things that are wrong.

I assume you copy-pasted the 'love' script from some other site/tutorial. When I open your site in firefug and look at the console, I get this:
enter image description here

I copy and pasted the <script> bit from my firefox source view into notepad++ and got this:

<script type="text/javascript">
jQuery(function() {
    jQuery('#like-it').click(function() {
        jQuery('#like-it').load('wp-content/themes/dreamsynk/value.php');
    });
});?         // <---------- that question mark should not be there!
</script> 

Perhaps when you copy-pasted the script you also copied some hidden/bad symbols along with it.

UPDATE
Changing character encoding from UTF-8 to Western 8859-1 gets me this:
enter image description here

于 2012-09-23T14:14:53.717 回答
0

呃,我刚刚试了下你页面的脚本,但是好像不能正常工作,只有“倒计时”,没有“倒计时”,也没有控制IP,让一个访问者“爱”你的页面几次,这也很糟糕。

你可以看看这个教程。它可以帮助您:

http://blog.deepscripts.com/how-to-build-a-like-my-page-script/

于 2012-10-07T07:15:25.033 回答