11/04/2012, 18:00
|
| | Fecha de Ingreso: marzo-2012
Mensajes: 17
Antigüedad: 12 años, 1 mes Puntos: 0 | |
Respuesta: Crear un boton para Votar Cita:
Iniciado por johhan16 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 > 0 { $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 |