Quite and old post but this is how you may set this into work with PHP
. I am not sure about wordpress but this should give you a good start point to customize as per your needs.
- Get the HTML bit ready (
home.html
)
<html><head><title>Voting Machine</title>
<link rel="stylesheet" type="text/css" href="vote/upvote.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="event.js"></script>
<script type="text/javascript" src="vote/upvote.js"></script>
</head><body>
<div id="topic-123" class="upvote">
<a class="upvote" title="This idea is helpful"></a>
<span class="count">5</span>
<a class="downvote"></a>
<a class="star starred"></a>
</div>
<div id="message"></div>
</body>
</html>
I have explained about event.js
below
- Set the PHP file to execute the server side (
insert.php
)
<?php
$con = mysql_connect("localhost","root","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ccy", $con);
$code=$_POST['id'];
$up=$_POST['up'];
$down=$_POST['down'];
$star=$_POST['star'];
$query=mysql_query("INSERT INTO mytable(code,up,down,star) VALUES('$code','$up','$down','$star')");
if($query){
echo "Data for $name inserted successfully!";
}
else{ echo "An error occurred!" . mysql_error(); }
?>
- Last but not the least, you need to get the values from the DIV and
pass it on to MySQL (or any other database). This is done using
Ajax
post
. So in my event.js
I have following code
$(document).ready(function(){
var callback = function(data) {
$.ajax({
url: 'insert.php',
type: 'post',
data: { id: data.id, up: data.upvoted, down: data.downvoted, star: data.starred },
success: function(data) {
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(1500);
}
});
};
$('#topic-123').upvote({id: 123, callback: callback});
});
Not to forget that the MySQL table structure is like this
CREATE TABLE `mytable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` bigint(20) DEFAULT NULL,
`up` varchar(45) DEFAULT NULL,
`down` varchar(45) DEFAULT NULL,
`star` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=129 DEFAULT CHARSET=utf8;
Have fun!!!