Am gasit pe net un script free pentru un sondaj de opinie si l-am modificat dupa necesitati. Scriptul a functionat fara probleme dar am vrut sa mai imbunatatesc ceva la el si nu mai functioneaza. poate ma ajuta cineva de aici sa imi spuna unde am gresit.
Am un fisier function4.php si un pool.js (detaliate mai jos).
Formularul din function4.php este apelat in pagina principala asa: <?php getPoll(1);?> .
Acum, problema a aparut in momentul in care am vrut sa fac o modificare ca la incarcarea paginii sa se faca o verificare daca ip-ul a mai votat sau nu. Daca a mai votat, pe vutonul de vot sa apara un text cu ati mai votat - vezi rezultatele, dar nu mai functioneaza pool.js la apasarea butonului.
Nu stiu daca am fost destul de explicit.
Va rog sa ma ajutati ca nu mai stiu ce sa ii fac.
function4.php:
Cod: Selectaţi tot
require("db.php");
//GETTING VARIABLES START
$action = mysql_real_escape_string($_POST['action']);
$pollAnswerID = mysql_real_escape_string($_POST['pollAnswerID']);
$ip = $_SERVER['REMOTE_ADDR'];
//GETTING VARIABLES END
function getPoll($pollID){
//echo $query;jquery
$ip = $_SERVER['REMOTE_ADDR'];
$query2="select count(1) votari from poolIP where pollAnswerIP = '".$ip."'";
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2))
{
$x = $row2['votari'];
if($x=='0')
{
$query = "SELECT * FROM polls LEFT JOIN pollAnswers ON polls.pollID = pollAnswers.pollID WHERE polls.pollID = " . $pollID . " ORDER By pollAnswerListing ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$pollQuestion = $row['pollQuestion'];
$pollAnswerID = $row['pollAnswerID'];
$pollAnswerValue = $row['pollAnswerValue'];
if ($pollStartHtml == '') {
$pollStartHtml = '<div id="pollWrap"><form name="pollForm" method="post" action="inc/functions4.php?action=vote"><h3>' . $pollQuestion .'</h3><ul>';
$pollEndHtml = '</ul><input type="submit" name="pollSubmit" id="pollSubmit" value="Voteaza" /> <span id="pollMessage"></span><img src="ajaxLoader.gif" alt="Ajax Loader" id="pollAjaxLoader" /></form></div>';
}
$pollAnswersHtml = $pollAnswersHtml . '<li><input name="pollAnswerID" id="pollRadioButton' . $pollAnswerID . '" type="radio" value="' . $pollAnswerID . '" /> ' . $pollAnswerValue .'<span id="pollAnswer' . $pollAnswerID . '"></span></li>';
$pollAnswersHtml = $pollAnswersHtml . '<li class="pollChart pollChart' . $pollAnswerID . '"></li>';
}
echo $pollStartHtml . $pollAnswersHtml . $pollEndHtml;
} else {
$query = "SELECT * FROM polls LEFT JOIN pollAnswers ON polls.pollID = pollAnswers.pollID WHERE polls.pollID = " . $pollID . " ORDER By pollAnswerListing ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$pollQuestion = $row['pollQuestion'];
$pollAnswerID = $row['pollAnswerID'];
$pollAnswerValue = $row['pollAnswerValue'];
if ($pollStartHtml == '') {
$pollStartHtml = '<div id="pollWrap"><form name="pollForm" method="post" action="inc/functions4.php?action=getResults"><h3>' . $pollQuestion .'</h3><ul>';
$pollEndHtml = '</ul><input type="submit" name="showResults" id="showResults" value="Ati mai votat - vezi rezultatele" /> <span id="pollMessage"></span><img src="ajaxLoader.gif" alt="Ajax Loader" id="pollAjaxLoader" /></form></div>';
}
$pollAnswersHtml = $pollAnswersHtml . '<li><input name="pollAnswerID" id="pollRadioButton' . $pollAnswerID . '" type="radio" value="' . $pollAnswerID . '" /> ' . $pollAnswerValue .'<span id="pollAnswer' . $pollAnswerID . '"></span></li>';
$pollAnswersHtml = $pollAnswersHtml . '<li class="pollChart pollChart' . $pollAnswerID . '"></li>';
}
echo $pollStartHtml . $pollAnswersHtml . $pollEndHtml;
}
}
}
function getPollID($pollAnswerID){
$query = "SELECT pollID FROM pollAnswers WHERE pollAnswerID = ".$pollAnswerID." LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
return $row['pollID'];
}
function getPollResults($pollID){
$colorArray = array(1 => "#ffcc00", "#00ff00", "#cc0000", "#0066cc", "#ff0099", "#ffcc00", "#00ff00", "#cc0000", "#0066cc", "#ff0099");
$colorCounter = 1;
$query = "SELECT pollAnswerID, pollAnswerPoints FROM pollAnswers WHERE pollID = ".$pollID."";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
if ($pollResults == "") {
$pollResults = $row['pollAnswerID'] . "|" . $row['pollAnswerPoints'] . "|" . $colorArray[$colorCounter];
} else {
$pollResults = $pollResults . "-" . $row['pollAnswerID'] . "|" . $row['pollAnswerPoints'] . "|" . $colorArray[$colorCounter];
}
$colorCounter = $colorCounter + 1;
}
$query = "SELECT SUM(pollAnswerPoints) FROM pollAnswers WHERE pollID = ".$pollID."";
$result = mysql_query($query);
$row = mysql_fetch_array( $result );
$pollResults = $pollResults . "-" . $row['SUM(pollAnswerPoints)'];
echo $pollResults;
}
//VOTE START
if ($action == "vote"){
if (isset($_COOKIE["poll" . getPollID($pollAnswerID)])) {
echo "voted";
} else {
$query="select count(1) votari from poolIP where pollAnswerIP = '".$ip."'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$x = $row['votari'];
if($x=='0')
{
$query = "INSERT INTO `poolIP` SET pollAnswerID = ".$pollAnswerID.", pollID = 1, pollAnswerValue = 'aaa', pollAnswerIP = '".$ip."'";
mysql_query($query) or die('Error, insert query failed');
$query = "UPDATE pollAnswers SET pollAnswerPoints = pollAnswerPoints + 1 WHERE pollAnswerID = ".$pollAnswerID;
mysql_query($query) or die('Error, insert query failed');
setcookie("poll" . getPollID($pollAnswerID), 1, time()+259200, "/");
getPollResults(1);
} else {
echo "voted";
}
}
}
}
//VOTE END
if ($action == "getResults"){
getPollResults(1);
}
pool.js:
Cod: Selectaţi tot
$(document).ready(function() {
$("#pollAjaxLoader").hide(); //hide the ajax loader
$("#pollMessage").hide(); //hide the ajax loader
$("#pollSubmit").click(function() {
var pollAnswerVal = $('input:radio[name=pollAnswerID]:checked').val();//Getting the value of a selected radio element.
if ($('input:radio[name=pollAnswerID]:checked').length) {
$("#pollAjaxLoader").show(); //show the ajax loader
$.ajax({
type: "POST",
url: "inc/functions4.php",
data: { pollAnswerID: pollAnswerVal, action: "vote" },
success: function(theResponse) {
//the functions.php returns a response like "1|13|#ffcc00-2|32|#00ff00-3|18|#cc0000-63" which the first number is the answerID, second is the points it has and third is the color for that answer's graph. The last number is the sum of all points for easilt calculating percentages.
if (theResponse == "voted") {
$("#pollAjaxLoader").hide(); //hide the ajax loader
$("#pollMessage").html("ne pare rau, ati mai votat.").fadeTo("slow", 1);
} else {
var numberOfAnswers = (theResponse).split("-").length-2;//calculate the number of answers
var splittedResponse = (theResponse).split("-");
var pollAnswerTotalPoints = splittedResponse[numberOfAnswers+1];
for (i=0;i<=numberOfAnswers;i++)
{
var splittedAnswer = (splittedResponse[i]).split("|");
var pollAnswerID = (splittedAnswer[0]);
var pollAnswerPoints = (splittedAnswer[1]);
var pollAnswerColor = (splittedAnswer[2]);
var pollPercentage = (100 * pollAnswerPoints / pollAnswerTotalPoints);
$(".pollChart" + pollAnswerID).css("background-color",pollAnswerColor);
$(".pollChart" + pollAnswerID).animate({width:pollPercentage + "%"});
$("#pollAnswer" + pollAnswerID).html(" (" + Math.round(pollPercentage) + "% - " + pollAnswerPoints + " votes)");
$("#pollRadioButton" + pollAnswerID).attr("disabled", "disabled"); //disable the radio buttons
}
$("#pollAjaxLoader").hide(); //hide the ajax loader again
$("#pollSubmit").attr("disabled", "disabled"); //disable the submit button
}
}
});
return false;
} else {
$("#pollMessage").html("va rugam selectati un raspuns.").fadeTo("slow", 1, function(){
setTimeout(function() {
$("#pollMessage").fadeOut("slow");
}, 3000);
});
return false;
}
});
//-----RESULT
$(".showResults").click(function() {
$("#pollAjaxLoader").show(); //show the ajax loader
$.ajax({
type: "POST",
url: "inc/functions4.php",
data: { pollAnswerID: pollAnswerVal, action: "getResults" },
success: function(theResponse) {
var numberOfAnswers = (theResponse).split("-").length-2;//calculate the number of answers
var splittedResponse = (theResponse).split("-");
var pollAnswerTotalPoints = splittedResponse[numberOfAnswers+1];
for (i=0;i<=numberOfAnswers;i++)
{
var splittedAnswer = (splittedResponse[i]).split("|");
var pollAnswerID = (splittedAnswer[0]);
var pollAnswerPoints = (splittedAnswer[1]);
var pollAnswerColor = (splittedAnswer[2]);
var pollPercentage = (100 * pollAnswerPoints / pollAnswerTotalPoints);
$(".pollChart" + pollAnswerID).css("background-color",pollAnswerColor);
$(".pollChart" + pollAnswerID).animate({width:pollPercentage + "%"});
$("#pollAnswer" + pollAnswerID).html(" (" + Math.round(pollPercentage) + "% – " + pollAnswerPoints + " votes)");
$("#pollRadioButton" + pollAnswerID).attr("disabled", "disabled"); //disable the radio buttons
}
$("#pollAjaxLoader").hide(); //hide the ajax loader again
$("#showResults").attr("disabled", "disabled"); //disable the submit button
}
});
return false;
});
});