Foros del Web » Programando para Internet » Javascript »

Recoger variables desde un formulario dinamco

Estas en el tema de Recoger variables desde un formulario dinamco en el foro de Javascript en Foros del Web. Código: <html> <head> <title>Crear Campo de texto</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <script type="text/javascript"> icremento =0; function crear(obj) { icremento++; field = document.getElementById('field'); contenedor = ...
  #1 (permalink)  
Antiguo 13/03/2013, 02:55
 
Fecha de Ingreso: marzo-2013
Mensajes: 10
Antigüedad: 11 años, 1 mes
Puntos: 0
Recoger variables desde un formulario dinamco

Código:
<html> 
<head>
<title>Crear Campo de texto</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
 
icremento =0;
function crear(obj) {
  icremento++;
  
  field = document.getElementById('field'); 
 contenedor = document.createElement('div'); 
  contenedor.id = 'div'+icremento; 
  field.appendChild(contenedor); 
 
  boton = document.createElement('input'); 
  boton.type = 'text'; 
  boton.name = 'text'+'[]'; 
  contenedor.appendChild(boton); 
   
  boton = document.createElement('input'); 
  boton.type = 'button'; 
  boton.value = 'Borrar'; 
  boton.name = 'div'+icremento; 
  boton.onclick = function () {borrar(this.name)} //aqui llamamos a la funcion borrar
  contenedor.appendChild(boton); 
  return contenedor.id;
}
function borrar(obj) {//aqui la ejecutamos
  field = document.getElementById('field'); 
  field.removeChild(document.getElementById(obj)); 
}
</script>


</head>
<body>
<form name="form1" method="POST" action="validar5.php">
 
<fieldset id="field">
  <p>
  <input type="button" value="Crear caja de texto" onClick="crear(this)">
  <input name="send" type="submit" value="Enviar" onClick="enviar(this)" id="send">
  </p>
</fieldset>
</form>
Este es el código que uso y para leer las variables me recomendaron:

Código PHP:

<?php

echo "<pre>";
print_r($_POST);
echo 
"</pre>";

$_POST=array();
 
 
$camp1=$_POST[0];
 
 echo 
"$camp1";

?>

Y obtengo esto como resultado:


Array
(
[send] => Enviar
[text] => Array
(
[0] => campo1
[1] => campo2
[2] => campo3
)

)


Intento leer utillizando :

Código PHP:

$camp1
=$_POST[0];
 
 echo 
"$camp1"
pero me sale:
Notice: Undefined offset: 0

que hago???
  #2 (permalink)  
Antiguo 13/03/2013, 04:29
Avatar de IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: Recoger variables desde un formulario dinamco

este es un problema que está relacionado con php, no con javascript

frm.php
Cita:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta name="http-equiv" content="Content-type: text/html; charset=UTF-8"/>
</head>
<body>
<form action="frm.php" method="post">
<input type="text" name="campos[]" value="valor1" />
<input type="text" name="campos[]" value="valor2" />
<input type="text" name="campos[]" value="valor3" />
<br />
<input type="submit" value="Enviar" />
</form>
<?php
$valores = array($_POST['campos']); // matriz con todos los valores de los campos


if(sizeof($valores[0]) != 0) {

foreach($valores[0] as $key => $valor) {
$fields[] = $key;
$values[] = $valor;
}

echo "Esta es la información recibida<br /><br />";
echo "+ CAMPOS " .implode(',', $fields). " VALORES " .implode(',', $values);

}
?>
</body>
</html>
no importa si los controles se crean dinámicamente o no
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}

Etiquetas: formulario, funcion, html, input, php, recoger, variables
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 21:19.