View Full Version : How's is this done? PHP? AJAX?
SChaput
10-19-2009, 11:05 PM
I'm curious as to how the website, http://www.textsfromlastnight.com handles their rating scale.
When your on any given page you can click "good night" or "bad night" depending on your vote it increments by one and doesn't let you vote on that particular post again.
Is this done by using AJAX and PHP combined with a mysql database, using cookies and IP's to block repeat voters?
If anyone knows of any tutorials I can view to try to implement something like this, I would really appreciate it.
Thanks.
*
JShor
10-20-2009, 10:03 PM
Exactly as you said. Disables repeat voters by storing IPs, stores counts in a datasource, uses cookies to block repeat voters, and uses AJAX to post.
I did the same project. Here's my coden:
vote.php:
<script type="text/javascript" src="js/voting.js"></script>
<?php
$voteQ = mysql_query("SELECT * FROM votes WHERE ip='$_SERVER[REMOTE_ADDR]' AND msgID='$ID'") or die(mysql_error());
if(mysql_num_rows( $voteQ ) > 0) {
$rvq = mysql_fetch_array( $voteQ );
if($rvq[voteType] == 'Yes') {
$yes = 'voteYes.png';
$no = 'voteNoDisabled.png';
} else {
$yes = 'voteYesDisabled.png';
$no = 'voteNo.png';
}
}
if($row[topicMsg] == 'Suggestions') {
?>
<?php
if(mysql_num_rows( $voteQ ) > 0) {
?>
<img src="images/<?=$yes ?>" alt=" You voted on this suggestion ">
<img src="images/<?=$no ?>" alt=" You voted on this suggestion ">
<?php
} else {
?>
<a href="javascript:vote('Yes')">
<img src="images/voteYes.png" alt=" Yes " border="0"></a>
<a href="javascript:vote('No')">
<img src="images/voteNo.png" alt=" No " border="0"></a>
<?php
}
?>
voting.js
var http_request = false;
function makePOSTRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById('myspan').innerHTML = '<img src="../images/accept.png" alt=""> <font size="1" color="Green"><b>Vote Successfully Submitted</b></font>';
if(document.getElementById("vote").value == 'Yes') {
document.getElementById('thumbs').innerHTML = '<img src="../images/voteYes.png" alt=" You voted on this suggestion "> <img src="../images/voteNoDisabled.png" alt=" You voted on this suggestion ">';
} else {
document.getElementById('thumbs').innerHTML = '<img src="../images/voteYesDisabled.png" alt=" You voted on this suggestion "> <img src="../images/voteNo.png" alt=" You voted on this suggestion ">';
}
} else {
document.getElementById('myspan').innerHTML = '<img src="../images/decline.png" alt=""> <font size="1" color="#FF0000"><b>Vote Unsuccessfully Submitted</b></font>';
}
}
}
function get(obj) {
var poststr = "vote=" + encodeURI( document.getElementById("vote").value ) + "&ID=" + encodeURI( document.getElementById("ID").value ) + "&IP=" + encodeURI( document.getElementById("IP").value );
makePOSTRequest('../voteMsg.php', poststr);
}
function vote(voteType) {
document.getElementById('vote').value = voteType;
get(this.parentNode);
document.getElementById('myspan').innerHTML = '<img src="../images/gif/loading.gif" alt=""> <font size="1"><b>Submitting vote...</b></font>';
}
voteMsg.php
<?php
include('lib/db_connect.php');
if(isset($_POST[IP])) {
if(isset($_POST[ID])) {
if(isset($_POST[vote])) {
mysql_query("INSERT INTO votes (ip, msgID, voteType) VALUES('$_POST[IP]', '$_POST[ID]', '$_POST[vote]') ") or die(mysql_error());
}
}
}
?>
Sorry for the code not being explained, let me know if you need hulp.
HTH:)
SChaput
11-23-2009, 02:23 AM
Sorry for the delayed response. I've had some busy times and not alot of free time, but still very interested in getting this example working. I am having issues trying to work out your code, is there some sort of online tutorial thats any simpler but still blocks via IP etc?
Thanks
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.