Foros del Web » Programando para Internet » PHP »

Problema con checkbox y su prop Checked

Estas en el tema de Problema con checkbox y su prop Checked en el foro de PHP en Foros del Web. Hola que tal a todos....Les cuento que estoy haciendo una web de tarea para la facultad y tengo el siguiente problema: Traigo de una BD ...
  #1 (permalink)  
Antiguo 14/06/2011, 19:22
Avatar de peketin  
Fecha de Ingreso: junio-2011
Ubicación: La Plata, Buenos Aires, Argentina
Mensajes: 4
Antigüedad: 12 años, 10 meses
Puntos: 0
Problema con checkbox y su prop Checked

Hola que tal a todos....Les cuento que estoy haciendo una web de tarea para la facultad y tengo el siguiente problema:

Traigo de una BD las caracteristicas de un auto.
listo todas las caracteristicas(de otra consulta * from Caracteristicas) y donde ese id exista en las aplicadas al auto las pone como CHECKED.
El problema que tengo es que si el usuario quiere quitar una propiedad del auto cuando tomo los checkbox al parece siempre me sigue tomando esa propiedad checked.

Codigo:
AQUI EL FORMULARIO:
Código PHP:
$consulta=mysql_query("SELECT * FROM Caracteristicas");
$existente=mysql_query('Select idCaracteristica from Vehiculos_Caracteristicas where Vehiculos_Caracteristicas.idVehiculo='.$mi_array['idVehiculo'].'');    
while(
$reg=mysql_fetch_array($consulta)){
      
$ok=true;
      
$existente=mysql_query('Select idCaracteristica from Vehiculos_Caracteristicas where Vehiculos_Caracteristicas.idVehiculo='.$mi_array['idVehiculo'].'');    
      while(
$existe=mysql_fetch_array($existente)){
        if (
$reg[0]==$existe['idCaracteristica']){
        echo 
'<input type="checkbox" checked="1" name="carac[]" value="'.$reg[0].'">'.$reg[1].'</br>';                
        
$ok=false;
            }
       }
       if (
$ok){echo'<input type="checkbox" name="carac[]" value="'.$reg[0].'">'.$reg[1].'</br>';
    }

ACA COMPRUEBO
Código PHP:
if(isset($_POST['carac'])){
    foreach(
$_POST['carac'] as $value){
        if (
$value==true){
            if (
$value==5){ echo'aca entro por nacional verdadero';}
                
$ok=true;
                
$yata=true;
                
$existente=mysql_query('Select idCaracteristica from Vehiculos_Caracteristicas where Vehiculos_Caracteristicas.idVehiculo='.$idveh.'');
                while((
$existe=mysql_fetch_row($existente)) and ($yata)){
                    if (
$value==$existe[0]){
                        
$yata=false;
                        
$ok=true;
                }
                else{
                    
$ok=false;
                }
                    }
                if (!(
$ok)){
                    @
mysql_query("Insert Into Vehiculos_Caracteristicas (`idVehiculoCaractesristica`,`idVehiculo`,`idCaracteristica`) values (null,'".$idveh."','".$value."')");    
                }
                        
            }
            else{
                
$existente=mysql_query('Select idCaracteristica from Vehiculos_Caracteristicas where Vehiculos_Caracteristicas.idVehiculo='.$idveh.'');
                while(
$existe=mysql_fetch_row($existente)){    
                    if (
$value==$existe[0]){
                        @
mysql_query('DELETE from Vehiculos_Caracteristicas where idVehiculo='.$idveh.' and idCaracteristica='.$value.'');
                    }
                }
            }
        }
    } 
  #2 (permalink)  
Antiguo 14/06/2011, 23:46
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: Problema con checkbox y su prop Checked

Prueba haciendo var_dump($_POST['carac']); asi podras saber como manejar los valores obtenidos.
__________________
- León, Guanajuato
- GV-Foto
  #3 (permalink)  
Antiguo 15/06/2011, 03:19
 
Fecha de Ingreso: febrero-2010
Mensajes: 295
Antigüedad: 14 años, 2 meses
Puntos: 58
Respuesta: Problema con checkbox y su prop Checked

Cita:
Iniciado por peketin Ver Mensaje
ACA COMPRUEBO
Código PHP:
if(isset($_POST['carac'])){
    foreach(
$_POST['carac'] as $value){
        if (
$value==true){
            if (
$value==5){ echo'aca entro por nacional verdadero';}
                
$ok=true;
                
$yata=true;
                
$existente=mysql_query('Select idCaracteristica from Vehiculos_Caracteristicas where Vehiculos_Caracteristicas.idVehiculo='.$idveh.'');
                while((
$existe=mysql_fetch_row($existente)) and ($yata)){
                    if (
$value==$existe[0]){
                        
$yata=false;
                        
$ok=true;
                }
                else{
                    
$ok=false;
                }
                    }
                if (!(
$ok)){
                    @
mysql_query("Insert Into Vehiculos_Caracteristicas (`idVehiculoCaractesristica`,`idVehiculo`,`idCaracteristica`) values (null,'".$idveh."','".$value."')");    
                }
                        
            }
            else{
                
$existente=mysql_query('Select idCaracteristica from Vehiculos_Caracteristicas where Vehiculos_Caracteristicas.idVehiculo='.$idveh.'');
                while(
$existe=mysql_fetch_row($existente)){    
                    if (
$value==$existe[0]){
                        @
mysql_query('DELETE from Vehiculos_Caracteristicas where idVehiculo='.$idveh.' and idCaracteristica='.$value.'');
                    }
                }
            }
        }
    } 
Yo creo que tienes errores en la colocación de las llaves. Si están bien colocadas, dudo que el script funcione como quieres. Para empezar:

if ($value==true){

Esto se va a cumplir siempre porque el array $_POST['carac'] tiene valores ya que lo indicas aquí:

if(isset($_POST['carac'])){

por tanto en el else no va a entrar nunca. Siempre entrará en el if. El else correspondiente a ese if, según las llaves que tienes puestas, sería este:

Código PHP:
else{
                
$existente=mysql_query('Select idCaracteristica from Vehiculos_Caracteristicas where Vehiculos_Caracteristicas.idVehiculo='.$idveh.'');
                while(
$existe=mysql_fetch_row($existente)){    
                    if (
$value==$existe[0]){
                        @
mysql_query('DELETE from Vehiculos_Caracteristicas where idVehiculo='.$idveh.' and idCaracteristica='.$value.'');
                    }
                }
            } 
A este else, como digo no entraría nunca, según entiendo yo el script.

Comprueba que las llaves las tengas bien puestas, antes de nada.

Ten en cuenta que aquí abres y cierras llave:

if ($value==5){ echo'aca entro por nacional verdadero';}

Etiquetas: checkbox, checked
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 19:31.