Ver Mensaje Individual
  #1 (permalink)  
Antiguo 28/03/2012, 06:40
jazhiels
 
Fecha de Ingreso: marzo-2012
Mensajes: 6
Antigüedad: 12 años, 1 mes
Puntos: 0
Exclamación Evento onclick (Ajax + PHP) SOLO 1 VEZ

Hola a todos..

Bueno, hace ya algunas semanas estoy montando una web en la cual he incorporado un sistema de votaciones para cada entrada qe tengo almacenada en una base de datos.. ahora mi script hace las votaciones correctamente PERO yo quiero que una persona solo pueda votar en una misma entrada una sola vez!

YA SE UNA VEZ EN POSITIVO Y UNA VEZ EN NEGATIVO , pero solo una vez por cada post ( es decir que puede votar una vez en los miles de posts que haya).

Os dejo basicamente la parte de html de las votaciones + el ajax + php

Si alguien pudiera hecharme una mano en eso os lo agradeceria xqe ando ya mucho tiempo con esto..

index.php

Código PHP:
 <?php
                        
include ('config.php');

$query mysql_query("SELECT * FROM imagestore ORDER BY thumbs_up - thumbs_down DESC LIMIT 5");

// como este codigo va en el index, muestro los 5 mejores posts

while($row=mysql_fetch_array($query))
{
        
        
$id=$row['id'];
            
    echo                    
'<ul>
                                
                                    <li onclick="posthumb('
.$id.');">'.$tu.' THUMBS UP</li>
                                    <li onclick="negthumb('
.$id.')">'.$td.' THUMBS DOWN</li>
                                    </ul>
'
;}
?>
functions.js
Código:
var xmlHttp;
function negthumb(str)
{ 

xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
 		{
		 alert ("Browser does not support HTTP Request");
		 return;
 		}
		
var url="voteneg.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}




function posthumb(str)
{ 

xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
 		{
		 alert ("Browser does not support HTTP Request");
		 return;
 		}
		
var url="votepos.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

y x ultimo los archivos votepos.php y voteneg.php donde hago la query a la bd
votepos.php
Código PHP:
<?php

include ('config.php');
$q=$_GET["q"];
$sql="UPDATE imagestore SET thumbs_up=thumbs_up+1 WHERE id = '".$q."'";

$result mysql_query($sql);
?>
voteneg.php
Código PHP:
<?php

include ('config.php');
$q=$_GET["q"];
$sql="UPDATE imagestore SET thumbs_down=thumbs_down+1 WHERE id = '".$q."'";

$result mysql_query($sql);
?>

pd : Perdonad si no es el hilo correcto es que este script tiene un poco de todo y no sabia donde ponerlo, por eso lo puse en el hilo de los mas "PRO" :D

Saludos!