Ver Mensaje Individual
  #1 (permalink)  
Antiguo 11/09/2012, 14:12
Avatar de loka-por-racing
loka-por-racing
 
Fecha de Ingreso: noviembre-2002
Ubicación: buenos aires
Mensajes: 55
Antigüedad: 21 años, 6 meses
Puntos: 0
Pregunta PHP + ajax problema al insertar varias encuestas

buenas, tengo inconvenientes al tratar de insertar varias encuestas en una misma pagina. use el codigo de esta web: http://www.webresourcesdepot.com/aja...p-mysql-jquery
pero modifique un par de cosas ya que no me funcionaba ok. cuando dejo una sola todo funciona bien, perocuando quiero insertar varias en la misma pagina ahi ya empieza a funcionar mal. yo creo que el problema surge porque cada form que se crea en cada encuesta tienen los mismos nombres, ya que el dibujito del "loading" no desaparece nunca, pero intente modificar el archivo polls.js para que las variables terminen con el numero de cada id de encuesta pero cuando pruebo haciendo click en votar me pone la pagina en blanco y no hace nada.
les copio el codigo de todos los que utilizo a ver si alguien puede darme una mano.

base de datos:
Código:
-- ----------------------------
-- Table structure for pollanswers
-- ----------------------------
CREATE TABLE `pollanswers` (
  `pollAnswerID` int(11) NOT NULL AUTO_INCREMENT,
  `pollID` int(11) DEFAULT NULL,
  `pollAnswerValue` varchar(250) DEFAULT NULL,
  `pollAnswerPoints` int(11) DEFAULT NULL,
  `pollAnswerListing` int(11) DEFAULT NULL,
  PRIMARY KEY (`pollAnswerID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Table structure for polls
-- ----------------------------
CREATE TABLE `polls` (
  `pollID` int(11) NOT NULL AUTO_INCREMENT,
  `pollQuestion` varchar(250) DEFAULT NULL,
  `pollStatus` tinyint(4) DEFAULT NULL COMMENT '0:passive - 1:active',
  PRIMARY KEY (`pollID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
el codigo para insertar las encuestas en la pagina.php:
Código:
<?php 
getPoll(1); 
getPoll(2); 
?>
votacion/inc/functions.php
Código:
<?php
require("db.php");
//GETTING VARIABLES START
if (isset($_POST['action'])) {
	$action 		= mysql_real_escape_string($_POST['action']);
}
if (isset($_POST['pollAnswerID'])) {
	$pollAnswerID	= mysql_real_escape_string($_POST['pollAnswerID']); 
}
//GETTING VARIABLES END


function getPoll($pollID){
	//yy
	if (isset($_COOKIE["poll" . getPollID($pollAnswerID)])) {//ya votó
		echo "votó";
	} else {
	//yy
	$query  = "SELECT * FROM polls LEFT JOIN pollanswers ON polls.pollID = pollanswers.pollID WHERE polls.pollID = " . $pollID . " ORDER By pollAnswerListing ASC";
	$result = mysql_query($query);
	//echo $query;jquery
	
	$pollStartHtml = '';
	$pollAnswersHtml = '';
	
	
	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="votacion/inc/functions.php?action=vote"><h3 class="color1">' . $pollQuestion .'</h3><ul>';
			$pollEndHtml 	= '</ul><input type="submit" name="pollSubmit" id="pollSubmit" value="Votar" /> <span id="pollMessage"></span><img src="votacion/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;
	//yy
	}
	//yy
}

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)])) {
		getPollResults(getPollID($pollAnswerID));
	} else {
		$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, "/", ".sitio.com");
		getPollResults(getPollID($pollAnswerID));
	}
}
//VOTE END

if (mysql_real_escape_string($_GET['cleanCookie']) == 1){
	setcookie("poll", "", time()-3600, "/", ".sitio.com");
	header('Location: http://www.sitio.com');
}
?>
votacion/inc/js/polls.js
Código:
$(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: "./votacion/inc/functions.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").show();
						$("#pollMessage").html("usted ya votó!").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("seleccione una opción").fadeTo("slow", 1, function(){
				setTimeout(function() {
					$("#pollMessage").fadeOut("slow");
				}, 3000);																		 
			});
			return false;
		}
	
	});

});
tambien utiliza el jquery-1.4.2.min.js
espero alguien pueda ayudarme para ver como se puede arreglar este inconveniente.