Foros del Web » Programando para Internet » PHP »

verificar Checkbox esta sin marcar con if

Estas en el tema de verificar Checkbox esta sin marcar con if en el foro de PHP en Foros del Web. Hola a todos tengo un problema estoy realizando una inserción con php y ajax con una condición se ejecuta un insert este es el código ...
  #1 (permalink)  
Antiguo 20/01/2016, 08:35
 
Fecha de Ingreso: julio-2013
Mensajes: 309
Antigüedad: 10 años, 9 meses
Puntos: 2
verificar Checkbox esta sin marcar con if

Hola a todos tengo un problema

estoy realizando una inserción con php y ajax con una condición se ejecuta un insert

este es el código :

Código PHP:
<input type='checkbox' id="moraLibro" name="moraLibro" value="mora">

<
script type="text/javascript">

$(
document).ready(function() {
    $(
'.error').hide();
    $(
".eliminarp").click(function() {
        
        
//Obtenemos el valor del campo mora
        
var moralibro = $("input#moralibro").val();
        
//Construimos la variable que se guardará en el data del Ajax para pasar al archivo php que procesará los datos
            
var parent = $(this).parent().attr('id');
        var 
service = $(this).parent().attr('data');
        var 
dataString 'id='+service '&moralibro=' moralibro ;
        $.
ajax({
            
type"POST",
            
url"eliminarprestamo.php",
            
datadataString,
            
success: function() {            
                $(
'#delete-ok').empty();
                $(
'#delete-ok').append('<div class="correcto">El prestamo se a eliminado correctamente a id='+service+'.</div>').fadeIn("slow");
                $(
'#'+parent).fadeOut("slow");
                
//$('#'+parent).remove();
            
}
        });
    $(
'#categoria-registrar')[0].reset();


        return 
false;
    });
});


</script> 
<?php

este es el archivo donde realiza el insert

Código PHP:
if (isset($_POST['moralibro']))
{
 
$query "INSERT INTO multas_estudiante(cedula_estudiante) 
  VALUES ('12345')"
;
  
$result pg_query($query) or die('ERROR AL INSERTAR DATOS: ' pg_last_error());

he intentado con if (isset($_POST['moralibro'])) pero igual realiza la insercion sin marcar el Checkbox , tambien con if($_POST['moralibro'] != "")

No se que mas poner , ¿afecta en algo el ajax?

Le agradezco sus respuestas
  #2 (permalink)  
Antiguo 20/01/2016, 08:43
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: verificar Checkbox esta sin marcar con if

Tu problema es el código Javascript, porque jamás verificas si el checkbox está seleccionado, sólo usas su valor y lo pasas por ajax: siempre.
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #3 (permalink)  
Antiguo 20/01/2016, 09:00
 
Fecha de Ingreso: julio-2013
Mensajes: 309
Antigüedad: 10 años, 9 meses
Puntos: 2
Respuesta: verificar Checkbox esta sin marcar con if

Como verifico si el checkbox está seleccionado con javascript?
  #4 (permalink)  
Antiguo 20/01/2016, 09:22
 
Fecha de Ingreso: agosto-2010
Ubicación: santiago, CHILE
Mensajes: 564
Antigüedad: 13 años, 8 meses
Puntos: 9
Respuesta: verificar Checkbox esta sin marcar con if

validado con php

Código PHP:
Ver original
  1. <?php
  2.  
  3. if(is_null($_POST['chktest'])){
  4.     echo "es nulo";
  5.    
  6. }else{
  7.    
  8.     echo $_POST['chktest'];
  9. }
  10.  
  11. ?>
  12. <!DOCTYPE html>
  13. <html>
  14. <head></head>
  15. <body>
  16. <form action="" method="post">
  17.  
  18.     <input type="checkbox" name="chktest" value="check 1">
  19.     <input type="checkbox" name="chktest" value="check 2">
  20.    
  21.     <input type="submit" value="enviar">
  22.    
  23. </form>
  24. </body>
  25. </html>
  #5 (permalink)  
Antiguo 20/01/2016, 09:36
 
Fecha de Ingreso: agosto-2010
Ubicación: santiago, CHILE
Mensajes: 564
Antigüedad: 13 años, 8 meses
Puntos: 9
Respuesta: verificar Checkbox esta sin marcar con if

la validacion de javascript la saque por ahi, y me toco lo facil

Código HTML:
Ver original
  1. <!DOCTYPE html>
  2.  
  3.     function test(){
  4.        
  5.         formulario = document.getElementById("formulario");
  6.         for(var i=0; i<formulario.elements.length; i++) {
  7.           var elemento = formulario.elements[i];
  8.           if(elemento.type == "checkbox") {
  9.             if(!elemento.checked) {
  10.               //return false;
  11.             }else{
  12.                 alert(elemento.value);             
  13.             }
  14.           }
  15.         }      
  16.     }
  17.    
  18. </head>
  19. <form id="formulario" action="" method="post" onsubmit="test(); return false;">
  20.  
  21.     <input type="checkbox" name="chktest" value="check 1">
  22.     <input type="checkbox" name="chktest" value="check 2">
  23.    
  24.     <input type="submit" value="enviar">
  25.    
  26. </form>
  27. </body>
  28. </html>

Etiquetas: checkbox, marcar, variable, verificar
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 10:17.