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>❤</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.