Ver Mensaje Individual
  #2 (permalink)  
Antiguo 25/12/2008, 12:59
Avatar de hgp147
hgp147
 
Fecha de Ingreso: diciembre-2006
Ubicación: Buenos Aires, Argentina
Mensajes: 980
Antigüedad: 17 años, 4 meses
Puntos: 36
Respuesta: sumar con checkbox php

Hola flashtkd, proba esto:

Código PHP:
<form name="form1" method="post" action="">
  <p>
    <input type="checkbox" name="checkbox[]" value="5">
    5
    <br>
    <input type="checkbox" name="checkbox[]" value="10">
    10
    <br>
    <input type="checkbox" name="checkbox[]" value="20">
    20
</p>
  <p>
    <input type="submit" name="submit" value="Calcular">
</p>
</form>  




<?php

if(isset($_POST['submit'])){
 
$checkbox $_POST['checkbox']; // Devuelve un array que contiene los valores de los campos activos

$suma 0;



if(
count($checkbox) > 0){ // Me fijo si hay algun valor seleccionado, sino estaría el IF provocaría un warning del PHP

foreach($checkbox as $valor){ // Recorre el array

$suma += $valor// Va sumando los valores

}

}



echo 
$suma;
 
}



?>
__________________
Spread Firefox | Download Day 2008
¡Únete en nuestra misión para alcanzar el Record Guinness al software más descargado en 24 horas! http://www.spreadfirefox.com/es-ES/worldrecord/