Foros del Web » Programando para Internet » PHP »

Guardar varios checkbox en un solo valor

Estas en el tema de Guardar varios checkbox en un solo valor en el foro de PHP en Foros del Web. Hola lindos, como estan? Les dejo mi consulta, vengo ya hace harto tiempo dandole vueltas a esto, y como mis conocimientos de codigo son casi ...
  #1 (permalink)  
Antiguo 31/07/2012, 11:26
 
Fecha de Ingreso: junio-2012
Ubicación: En Chile :D
Mensajes: 44
Antigüedad: 11 años, 10 meses
Puntos: 0
Guardar varios checkbox en un solo valor

Hola lindos, como estan?

Les dejo mi consulta, vengo ya hace harto tiempo dandole vueltas a esto, y como mis conocimientos de codigo son casi nulos solo me manejo con Dreamweaver para hacer cosas... tengo una tabla dinamica, la cual por cada valor me entrega un checkbox. Hasta ahi, todo bien, el problema se presenta cuando quiero marcar mas de un checkbox, ya que en vez de guardar todos los registros marcados, solo me guarda uno.

Hasta aqui, espero haber explicado bien lo que tengo y donde esta mi problema, a lo cual tengo que agregar (y que tampoco se como hacer, ni se si es que puede hacerse) es que cada valor de los checkbox seleccionados sean guardados separados por una coma.

aca les dejo mi codigo, espero haber sido clara y que no sea complicado de solucionar.

Muchas gracias a todos!!!

Código PHP:
<?php require_once('Connections/formulario.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  if (
PHP_VERSION 6) {
    
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

$editFormAction $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
}

if ((foreach(
$_POST['idcat'] as $item)) {
     echo 
$item;
}

  
$updateSQL sprintf("UPDATE falla_cocina SET opcion=%s WHERE id1=%s",
                       
GetSQLValueString($_POST['idcat'][0], "text"),
                       
GetSQLValueString($_POST['idcat'][1], "int")); 

  
mysql_select_db($database_formulario$formulario);
  
$Result1 mysql_query($insertSQL$formulario) or die(mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin título</title>
</head>

<body>
<?
$rows
=mysql_query("select * from falla_calefon") or die(mysql_error());
?>
<form name="form1" id="form1" action="<?php echo $editFormAction?>" method="POST">
    <table>
        <tr>
            <th>
                ID
            </th>
            <th>
                Falla
            </th>
        </tr>
       <?
       
while($row=mysql_fetch_array($rows)){
       
?>
        <tr>
            <td><?php echo $row['id']?><input type="checkbox" name="idcat[]" id="idcat" value="<?php echo $row['id'];?>" /></td>
            <td><?php echo $row['id']?></td>
        </tr>
        <?php 
       
}
        
?>
        <tr>
            <td colspan="2">
                <input type="submit" value="Modificar" />
            </td>
        </tr>
     </table>
    <input type="hidden" name="MM_insert" value="form1" />
</form>
</body>
</html>
  #2 (permalink)  
Antiguo 31/07/2012, 11:39
Avatar de enlinea777  
Fecha de Ingreso: mayo-2008
Ubicación: frente al pc
Mensajes: 1.830
Antigüedad: 15 años, 11 meses
Puntos: 127
Respuesta: Guardar varios checkbox en un solo valor

donde recives las variables del formulario agrega esto al principio del codigo y pegas aqui el resultado;

Código PHP:
var_dump($_POST['idcat']); 
Saludos
  #3 (permalink)  
Antiguo 31/07/2012, 12:05
 
Fecha de Ingreso: junio-2012
Ubicación: En Chile :D
Mensajes: 44
Antigüedad: 11 años, 10 meses
Puntos: 0
Respuesta: Guardar varios checkbox en un solo valor

tengo el siguiente error...

"Parse error: syntax error, unexpected T_FOREACH in C:\xampp\htdocs\prueba.php on line 39"
  #4 (permalink)  
Antiguo 31/07/2012, 12:11
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Guardar varios checkbox en un solo valor

La sintaxis if (foreach()) no existe en php, para empezar.
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #5 (permalink)  
Antiguo 31/07/2012, 12:19
 
Fecha de Ingreso: enero-2011
Ubicación: DF
Mensajes: 898
Antigüedad: 13 años, 2 meses
Puntos: 155
Respuesta: Guardar varios checkbox en un solo valor

una ojeada a esto

http://www.desarrolloweb.com/articulos/1576.php

pa mandar varios datos en una variable
__________________
No me interesan ni las gracias ni los karmas , pero si que se genere conocimiento, si se dio con la respuesta por favor confirmalo
  #6 (permalink)  
Antiguo 31/07/2012, 12:20
 
Fecha de Ingreso: junio-2012
Ubicación: En Chile :D
Mensajes: 44
Antigüedad: 11 años, 10 meses
Puntos: 0
Respuesta: Guardar varios checkbox en un solo valor

Cita:
Iniciado por pateketrueke Ver Mensaje
La sintaxis if (foreach()) no existe en php, para empezar.
JAJA!!!!... por ahi debe ir el asunto ... como dije, no se de codigo, me manejo con lo muy basico y dreamweaver que me hace TODO el resto...

alguna otra pifia que corregir?

Le saco el if, y me da problema con el echo... ¬¬

Etiquetas: checkbox, formulario, html, mysql, registro, sql, tabla
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 15:13.