I have a table where I show users with a column / id of "state". Where you will have two values: color green (activo, estado=1)
or color red (inactivo, estado=0)
. I have the following code to change the state but it shows me the initial green color but it does not change me when I click on the button.
Where do I have the error?
index.php
<table class="table table-striped" border="2" cellspacing="3" cellpadding="3" style="font-size: 10pt"> <!-- table-hover === se selecciona la fila indicada con el raton -->
<center>
<!-- Indicamos el nombre de las columnas de la tabla a visualizar. -->
<!-- TABLE = crear una tabla TR = crear una fila TD = crear una columna -->
<tr>
<thead style="background-color:#A9F5A9">
<td>
<font face="verdana" color="blue"><b><center>ID</center></b></font>
</td>
<td>
<font face="verdana" color="blue"><b><center>Login</center></b></font>
</td>
<td>
<font face="verdana" color="blue"><b><center>Email</center></b></font>
</td>
<td>
<font face="verdana" color="blue"><b><center>Nombre</center></b></font>
</td>
<td>
<font face="verdana" color="blue"><b><center>Apellidos</center></b></font>
</td>
<td>
<font face="verdana" color="blue"><b><center>Ciudad</center></b></font>
</td>
<td>
<font face="verdana" color="blue"><b><center>Teléfono</center></b></font>
</td>
<td>
<font face="verdana" color="blue"><b><center>Fecha registro</center></b></font>
</td>
<td>
<font face="verdana" color="blue"><b><center>Estado</center></b></font>
</td>
<td>
<font face="verdana" color="blue"><b><center>Modificar</center></b></font>
</td>
<td>
<font face="verdana" color="blue"><b><center>Eliminar</center></b></font>
</td>
</center>
</thead>
</tr>
</center>
<?php
//Extraemos la informacion de ambas tablas relacionadas con el foreign key (ID_OBLIGATORIO).
$query = "SELECT * FROM usuarios_obligatorios JOIN usuarios_datos ON usuarios_obligatorios.ID_OBLIGATORIO = usuarios_datos.ID_OBLIGATORIO ORDER BY usuarios_obligatorios.ID_OBLIGATORIO ASC";
$result = mysql_query($query);
$numero = 0;
//Mientras existen datos, los agregamos en la tabla (mediante filas).
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td width=\"05%\"><font face=\"verdana\"><font size='2'>".$row["ID_OBLIGATORIO"]."</font></font></td>";
echo "<td width=\"10%\"><font face=\"verdana\"><font size='2'>".$row["login"]."</font></font></td>";
echo "<td width=\"10%\"><font face=\"verdana\"><font size='2'>".$row["email"]."</font></font></td>";
echo "<td width=\"10%\"><font face=\"verdana\"><font size='2'>".$row["nombre"]."</font></font></td>";
echo "<td width=\"10%\"><font face=\"verdana\"><font size='2'>".$row["apellidos"]."</font></font></td>";
echo "<td width=\"10%\"><font face=\"verdana\"><font size='2'>".$row["ciudad"]."</font></font></td>";
echo "<td width=\"10%\"><font face=\"verdana\"><font size='2'>".$row["telefono"]."</font></font></td>";
echo "<td width=\"10%\"><font face=\"verdana\"><font size='2'>".$row["fecha_registro"]."</font></font></td>";
echo "<td width=\"08%\">" ?>
<!-- Columna ESTADO del usuario. (estado verde/rojo). -->
<center>
<?php
$estado = 1;
$class = "activo";
?>
<button class="activar <?php echo $class;?>" data-estado="<?php echo $estado?>" style="height: 14px; width:14px;"></button>
</center>
<?php
echo "</td>";
echo "<td width=\"08%\">" ?>
<!-- Columna MODIFICAR usuarios. Nos redirige a modificar.php (formulario ya con los datos cargados) gracias a: <a href="modificar.php?Id=<?=$row["ID_OBLIGATORIO"]?>">
Al pasar el raton encima de cada modificar, abajo en el navegador podemos ver el valor de cada ID: osmar.fyb3/modificar.php?Id=7 -->
<center><a href="modificar.php?Id=<?=$row["ID_OBLIGATORIO"]?>"><img src="imagenes/modificar.png" height='24' width='26' onmouseover="this.src='imagenes/modificar_in.png';" onmouseout="this.src='imagenes/modificar.png';"></center> <?php echo "</td>";
echo "<td width=\"08%\">" ?>
<!-- Columna ELIMINAR usuarios (checkbox). Sabemos el valor de cada fila mediante: value="<?php echo $row['ID_OBLIGATORIO']; ?>"
Al dar en el boton ELIMINAR_SELECCIONADOS, los borraremos. Guardamos los marcados en un array llamado seleccionados[] cuyo
valor lo extraemos mediante: value="<?php echo $row['ID_OBLIGATORIO']; ?>"
-->
<center><img src="imagenes/eliminar.png" height='23' width='24' alt="Eliminar"> <input type="checkbox" name="seleccionados[]" id="seleccionados" value="<? echo $row['ID_OBLIGATORIO']; ?>">
</center> <?php echo "</td>";
echo "</tr>";
$numero++;
}
mysql_free_result($result);
?>
</table>
<!-- Boton ELIMINAR SELECCIONADOS -->
<div class="boton_eliminar" class="table-responsive" align="right">
<font face="verdana">
<b><input type="submit" style="width:200px; height:28px;" name="eliminar_seleccionados" id="eliminar_seleccionados" onclick="return confirm('¿Deseas realmente eliminar estos usuarios?');" value="Eliminar seleccionados"></b></font><br/><br/>
</div>
<?php
//Si no existen datos ($filas == 0), icono de la BBDD vacia.
}else{
?><br/><h1><b><font size="16"><center>BASE DE DATOS SIN DATOS</center></font></b></h1><br/><center><img src="imagenes/bbdd.png" height="180" width="160"/></center>
<?php
}
?>
</div>
</div>
</form>
<?php
//Aqui va el codigo del boton eliminar_seleccionados...
//Si pulsamos el boton "Eliminar seleccinados"...
if(isset($_POST['eliminar_seleccionados'])) {
if(empty($_POST['seleccionados'])) {
echo "<center><h2>No se ha seleccionado ningun usuario.</center></h2>";
}else{
foreach($_POST['seleccionados'] as $valor){
$res1 = mysql_query("DELETE FROM usuarios_obligatorios WHERE ID_OBLIGATORIO=".$valor."");
$res2 = mysql_query("DELETE FROM usuarios_datos WHERE ID_OBLIGATORIO=".$valor."");
$res3 = mysql_query("DELETE FROM usuarios_aficiones WHERE ID_OBLIGATORIO=".$valor."");
}
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}
?>
</body>
</html>
<script>
//Funcion al clickear el button del ESTADO.
$('.activar').click(function(){
//Recogemos el valor de la variable "data-estado".
var estado = $('.activar').attr('data-estado');
//Llamamos al cambiar_estado.php y le pasamos el valor a la funcion.
$.post('cambiar_estado.php', {estado:estado}, function(response){
//Dato = responde = 0, entonces desactivamos (color rojo).
if(response == 0){
$('.activar').removeClass("activo");
$('.activar').addClass("inactivo");
}else{
$('.activar').removeClass("inactivo");
$('.activar').addClass("activo");
}
//Retornamos de nuevo el dato.
$('.activar').attr('data-estado', response);
});
});
</script>
index.js
$('.activar').on('click', cambiarEstado);
function cambiarEstado () {
var estado = $(this).attr('data-estado');
$.post('cambiar_estado.php', {estado:estado});
}
change_state.php
<?php
$estado = $_POST['estado'];
if ($estado == '1'){
$dato = 0;
}
else{
$dato = 1;
}
?>