How to validate array checkbox

1

What a good day, someone could tell me how it can be done so that when I press the "DOWNLOAD" button if it is empty that does not redirect me to the other page and if at least there is a checkbox activated and press "DOWNLOAD DATA" if you redirect me to the page.

Thank you.

<form action="datos.php" method="post" name="myform"> 
      <table class="table table-hover">
            <tr>
                <th></th><th></th><th></th><th></th>
                <th><input type="submit" name="formSubmit" value="BAJAR DATOS" class="btn btn-primary btn-round"/></th>
            </tr>

                <tr class="header">
                    <th class="colu">nombre</th>
                    <th class="colu">apellido</th>
                    <th class="colu">edad</th>
                    <th class="colu">sangre</th>
                    <th class="colu">enfermedad</th>        
                    <th class="escoger">
                        <p class="float_lefty">Escoger Todos</p>
                        <input class="float_lefty" type="checkbox" id="selectall"/>
                    </th>
                </tr>

                        <?php
                       if($myrow = mysql_fetch_array($result2))
                       {
                       do
                        {
                         $cuenta_datos++;

                        ?>

                           <tr> 
                                  <th class="colu"><?php printf("%s",$myrow["nombre"]);?></th>
                                  <th class="colu"><?php printf("%s",$myrow["apellido"]);?></th>
                                  <th class="colu"><?php printf("%s",$myrow["edad"]);?></th>
                                  <th class="colu"><?php printf("%s",$myrow["sangre"]);?></th>
                                  <th class="colu"><?php printf("%s",$myrow["enfermedad"]);?></th>  
                                  <th><p class="float_lefty" >Escoger Dato</p><input type="checkbox" class="case" name="case[]" value="<?php printf("%s",$myrow["nombre"]);?>"></th>
                          </tr>

                        <?php     
                            }while ($myrow = mysql_fetch_array($result2));
                            }else {

                            } 
                            //printf($cuenta_eventos);
                         ?>  

                                 <tr>
                                 <th></th><th></th><th></th><th></th>
                                 <th><input type="submit" name="formSubmit" value="BAJAR DATOS" class="btn btn-primary btn-round"/></th>
                                 </tr>
    </table>
            </form> 
    
asked by Houdini 24.08.2016 в 20:32
source

2 answers

3

One way to do this can be to deactivate / activate the button if a checkbox is selected or not:

$(function(){
  $('input:checkbox').change(function(){
    if ($('input:checkbox:checked').length > 0) {
      $('input:submit').prop({disabled: false}); 
    } else {
      $('input:submit').prop({disabled: true}); 
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <input type="checkbox">primero<br>
  <input type="checkbox">segundo<br>
  <input type="checkbox">tercero
</div>
<input type="submit" value="bajar datos" disabled>
    
answered by 24.08.2016 / 21:13
source
0

If you want to simplify the @Shaz code a bit, you can do the following:

$(function(){
 $('input:checkbox').change(function(){
  $('input:submit').prop({
   disabled: $('input:checkbox:checked').length===0
  });
 });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <input type="checkbox">primero<br>
  <input type="checkbox">segundo<br>
  <input type="checkbox">tercero
</div>
<input type="submit" value="bajar datos" disabled>
    
answered by 11.04.2017 в 17:52