Foros del Web » Programando para Internet » Javascript » Frameworks JS »

Crear un boton para Votar

Estas en el tema de Crear un boton para Votar en el foro de Frameworks JS en Foros del Web. Hola que tal, quiero hacer un boton para q la gente vote un articulo , que al clikear no actualice la pagina , solo el ...
  #1 (permalink)  
Antiguo 11/03/2011, 18:44
Avatar de GAST0N  
Fecha de Ingreso: agosto-2010
Ubicación: Buenos Aires
Mensajes: 680
Antigüedad: 13 años, 8 meses
Puntos: 64
Exclamación Crear un boton para Votar

Hola que tal, quiero hacer un boton para q la gente vote un articulo , que al clikear no actualice la pagina , solo el voton y que le diga la cantidad de votos que tiene

algo asi como un boton "me gusta" de facebook o el boton de "seguir" de twitter..

tiene algun nombre ese "efecto" .. ??

por favor me podrian dar algun tutorial o guia para poder ver como se hace..

muchas gracias de ante mano..
__________________
Twitter: @GastonArnedo

Muerte a los <tr> y <td>
  #2 (permalink)  
Antiguo 12/03/2011, 13:33
 
Fecha de Ingreso: junio-2010
Ubicación: Venezuela, Zulia
Mensajes: 686
Antigüedad: 13 años, 10 meses
Puntos: 55
Respuesta: Crear un boton para Votar

bueno eso lo haces colocando el boton en un div y ejecutandolo con ajax

ej:

index.php

Código HTML:
<html>
<head>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
<!-- 
tu codigo aqui
-->
<?php 
//AQUI DEBES BUSCAR LA FORMA DE COLOCAR EL NUMERO
//DEL ARTICULO DONDE SE HARA LA VOTACION 
$articulo = 1;
?>
<div id="votar">
    <input type="submit" name="vota_articulo" id="vota_articulo" value="Votar" onClick="ajax_votar(<?php echo $articulo; ?>)">
</div>
</body>
</html> 
ajax.js

Código:
function objetoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
		   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
  		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function ajax_votar(articulo){
	
	
	divResultado1 = document.getElementById('votar');
	
		//instanciamos el objetoAjax
		ajax=objetoAjax();

		ajax.open("GET", "votar.php?articulo="+articulo);
		ajax.onreadystatechange=function() {
			

			if (ajax.readyState==4) {
				//mostrar resultados en esta capa
				divResultado1.innerHTML = ajax.responseText;

				divResultado1.style.display="block";

			}
		}
		//como hacemos uso del metodo GET
		//colocamos null
		ajax.send(null);
	//}
}
votar.php

Código PHP:
<?php 
//recibimos la informacion desde ajax.js
$articulo $_GET['articulo'];

//aqui debemos hacer una conexion a una base de datos ej:
include_once('cone.php');
$link=conectarse();


//revisamos si tiene datos anteriores el articulo
$sql mysql_query("select id_articulo from votacion where id_articulo = '$articulo'",$link);
$filas mysql_fetch_row($sql);

//si se encuentra algun registro de este articulo entonces buscamos cuantos votos tiene
if $filas {
$sql2 mysql_query("select id_articulo, votos from votacion where id_articulo = '$articulo'",$link);
$array mysql_fetch_array($sql2);

$votos $array['votos'];
$votos_total $votos 1;

//aqui actualizamos la tabla con el nuevo resultado
mysql_query("update votacion set votos = '$votos_total' where id_articulo = '$articulo' ",$link);

} else {
//insertamos el voto ejemplo tabla votacion
mysql_query("insert into votacion (id_articulo, votos) values ('$articulo', 1)",$link);
}

//hacemos una ultima consulta para ver el total de votos
$sql3 mysql_query("select id_articulo, votos from votacion where id_articulo = '$articulo'",$link);
$array3 mysql_fetch_array($sql3);

$votos3 $array3['votos'];

//aqui mostramos la informacion final en el div
echo $votos3;
echo 
"Han votado por este articulo";

?>


bueno hice todo el trabajo por ti porque no tenia nada que hacer jejeje

todo lo coloque en un mismo directorio, y si no te funciona primero puede ser porque no lo probe XD y segundo podria ser porque no estas haciendo bien la conexion a la base de datos
  #3 (permalink)  
Antiguo 11/04/2012, 17:59
 
Fecha de Ingreso: marzo-2012
Mensajes: 17
Antigüedad: 12 años, 1 mes
Puntos: 0
Información Respuesta: Crear un boton para Votar

amigo tienes un error en el archivo votar.php en la linea 15
  #4 (permalink)  
Antiguo 11/04/2012, 18:00
 
Fecha de Ingreso: marzo-2012
Mensajes: 17
Antigüedad: 12 años, 1 mes
Puntos: 0
Información Respuesta: Crear un boton para Votar

Cita:
Iniciado por johhan16 Ver Mensaje
bueno eso lo haces colocando el boton en un div y ejecutandolo con ajax

ej:

index.php

Código HTML:
<html>
<head>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
<!-- 
tu codigo aqui
-->
<?php 
//AQUI DEBES BUSCAR LA FORMA DE COLOCAR EL NUMERO
//DEL ARTICULO DONDE SE HARA LA VOTACION 
$articulo = 1;
?>
<div id="votar">
    <input type="submit" name="vota_articulo" id="vota_articulo" value="Votar" onClick="ajax_votar(<?php echo $articulo; ?>)">
</div>
</body>
</html> 
ajax.js

Código:
function objetoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
		   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
  		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function ajax_votar(articulo){
	
	
	divResultado1 = document.getElementById('votar');
	
		//instanciamos el objetoAjax
		ajax=objetoAjax();

		ajax.open("GET", "votar.php?articulo="+articulo);
		ajax.onreadystatechange=function() {
			

			if (ajax.readyState==4) {
				//mostrar resultados en esta capa
				divResultado1.innerHTML = ajax.responseText;

				divResultado1.style.display="block";

			}
		}
		//como hacemos uso del metodo GET
		//colocamos null
		ajax.send(null);
	//}
}
votar.php

Código PHP:
<?php 
//recibimos la informacion desde ajax.js
$articulo $_GET['articulo'];

//aqui debemos hacer una conexion a una base de datos ej:
include_once('cone.php');
$link=conectarse();


//revisamos si tiene datos anteriores el articulo
$sql mysql_query("select id_articulo from votacion where id_articulo = '$articulo'",$link);
$filas mysql_fetch_row($sql);

//si se encuentra algun registro de este articulo entonces buscamos cuantos votos tiene
if $filas {
$sql2 mysql_query("select id_articulo, votos from votacion where id_articulo = '$articulo'",$link);
$array mysql_fetch_array($sql2);

$votos $array['votos'];
$votos_total $votos 1;

//aqui actualizamos la tabla con el nuevo resultado
mysql_query("update votacion set votos = '$votos_total' where id_articulo = '$articulo' ",$link);

} else {
//insertamos el voto ejemplo tabla votacion
mysql_query("insert into votacion (id_articulo, votos) values ('$articulo', 1)",$link);
}

//hacemos una ultima consulta para ver el total de votos
$sql3 mysql_query("select id_articulo, votos from votacion where id_articulo = '$articulo'",$link);
$array3 mysql_fetch_array($sql3);

$votos3 $array3['votos'];

//aqui mostramos la informacion final en el div
echo $votos3;
echo 
"Han votado por este articulo";

?>


bueno hice todo el trabajo por ti porque no tenia nada que hacer jejeje

todo lo coloque en un mismo directorio, y si no te funciona primero puede ser porque no lo probe XD y segundo podria ser porque no estas haciendo bien la conexion a la base de datos

tienes un error en el archivo votar.php en la linea 15

Etiquetas: ajax, votar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:49.