Foros del Web » Programando para Internet » PHP »

Checkbox array

Estas en el tema de Checkbox array en el foro de PHP en Foros del Web. <!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&iacute;tulo</title> </head> <?php $link=mysql_connect("localhost","root","root"); $database="negocio"; mysql_select_db($database); $resultado=mysql_query("select ...
  #1 (permalink)  
Antiguo 12/07/2007, 18:26
 
Fecha de Ingreso: diciembre-2006
Mensajes: 127
Antigüedad: 17 años, 4 meses
Puntos: 1
Checkbox array

<!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&iacute;tulo</title>
</head>

<?php

$link=mysql_connect("localhost","root","root");

$database="negocio";

mysql_select_db($database);


$resultado=mysql_query("select fab from switches");

print "<table width=200 border=0>\n";

print "<form action=$PHP_SELF method=post>";

while($get_info=mysql_fetch_array($resultado))
{
/*
print("<td><input type=\"checkbox\" value=\"" . $get_info['fab'] . "\" name=\"check[]\"></td>\n");
*/
//echo '<input type="checkbox" name="naam" value="'.$get_info['fab'].'">'.'<br />';

?>

<input type="checkbox" value='<?= $get_info["fab"]; ?>' align="BASELINE" name="ch[]" />
<? echo $get_info['fab'];?>
<br>
<?
}

print "<input type=submit name=ok value=\"Enviar\" >";
print "</form>";
print "</table>\n";

mysql_free_result($resultado);
?>
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp; </p>

<body >


<table width="489" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="489" height="292">
<?php

mysql_select_db("negocio",$link);
$res=mysql_query("select *from switches");
$array[]=array();

do
{

$dato=$fila['fab'];
$array[$c]=$dato;
$c++;
}while($fila=mysql_fetch_array($res));


//echo $array[1];

mysql_free_result($res);

?>


&nbsp;</td>
</tr>
</table>

<?

if ($ok="Enviar")
{
if (isset($_POST['ch'])
foreach ($_POST['ch'] as $value){
print $value;
}
}
?>
</body>
</html>

Alguien sabe porque me marca error el foreach , quiero mostrar los valores seleccionados del checkbox....Gracias


quiero mostrarlo en la misma pagina.
  #2 (permalink)  
Antiguo 12/07/2007, 18:55
okram
Invitado
 
Mensajes: n/a
Puntos:
Re: Checkbox array

En vez de if ($ok="Enviar") pon if ($ok=="Enviar")

Código PHP:
if ($ok="Enviar")
{
if (isset(
$_POST['ch'])
foreach (
$_POST['ch'] as $value){
print 
$value;
}

prueba poniendo en vez de eso:
Código PHP:
if ($ok=="Enviar")
{
echo 
'<pre>';
print_r($_POST);
echo 
'</pre>';

y ves como esta compuesto el array global $_POST...

¿Que tipo de error te devuelve el foreach?

Un saludo,
  #3 (permalink)  
Antiguo 12/07/2007, 18:58
okram
Invitado
 
Mensajes: n/a
Puntos:
Re: Checkbox array

En esta parte de tu codigo:
Código PHP:
do
{

$dato=$fila['fab'];
$array[$c]=$dato;
$c++;
}while(
$fila=mysql_fetch_array($res)); 
Por qué simplemente no pones:
Código PHP:
do
{
$array[] = $fila['fab'];
}while(
$fila=mysql_fetch_array($res)); 
Funcionará igual, pero el código será más corto y menos tedioso

Un saludo,
  #4 (permalink)  
Antiguo 12/07/2007, 19:30
 
Fecha de Ingreso: diciembre-2006
Mensajes: 127
Antigüedad: 17 años, 4 meses
Puntos: 1
Re: Checkbox array

Gracias okram de nuevo, probare eso :

do
{
$array[] = $fila['fab'];
}while($fila=mysql_fetch_array($res));

Otra duda okram si te diste cuenta el form action esta bien?....para que me imprima sobre la misma pagina??....gracias de nuevo
  #5 (permalink)  
Antiguo 13/07/2007, 10:59
okram
Invitado
 
Mensajes: n/a
Puntos:
Re: Checkbox array

Para enviar el formulario a la misma pagina en la que se encuentra tienes dos opciones:

Dejar el action vacio:
Código HTML:
<form action="" method="post"> 
ó puedes imprimir el nombre del archivo con php, pero para eso es mejor que uses el array superglobal $_SERVER:
Código PHP:
print "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">"
Un saludo,
  #6 (permalink)  
Antiguo 13/07/2007, 18:40
 
Fecha de Ingreso: diciembre-2006
Mensajes: 127
Antigüedad: 17 años, 4 meses
Puntos: 1
Sonrisa Re: Checkbox array

Gracias okram....me sirvio saludos
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 08:29.