Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] porqué no funciona variable session

Estas en el tema de porqué no funciona variable session en el foro de PHP en Foros del Web. Tengo un formulario el cual mediante variable de sesion necesito guardar su valor, el caso es que la mando a imprimir para ver si obtiene ...
  #1 (permalink)  
Antiguo 18/11/2015, 21:41
 
Fecha de Ingreso: noviembre-2010
Mensajes: 47
Antigüedad: 13 años, 5 meses
Puntos: 1
porqué no funciona variable session

Tengo un formulario el cual mediante variable de sesion necesito guardar su valor, el caso es que la mando a imprimir para ver si obtiene valores y me sale el siguiente error:

Nota: al action no llama archivo externo ya que necesito usar variable en este mismo form

Undefined index: txtusuario in C:\wamp\www\variables.php on line 3

<?php
session_start(); //Iniciamos la sesion
$_SESSION['nombre'] = $_POST['txtusuario']; // aqui marca error
?>
<html>
<head>
</head>
<body>

<form action="" method="post">
Escribe tu Nickname:
<input type="text" size="25" name="txtusuario" />
<br />
<input type="submit" value="Grabar" name="subir" />
<?php

if (isset($_POST['subir'])) {
echo "escribiste :".$_SESSION['nombre'];
}
?>
</form>
</body>
</html>
  #2 (permalink)  
Antiguo 18/11/2015, 22:06
Avatar de gonzaherrera77  
Fecha de Ingreso: noviembre-2015
Mensajes: 60
Antigüedad: 8 años, 5 meses
Puntos: 7
Respuesta: porqué no funciona variable session

Buenas proba así

Código PHP:
Ver original
  1. <?php
  2.  
  3. session_start(); //Iniciamos la sesion
  4. if(isset($_POST['txtusuario'])) {
  5.    $_SESSION['nombre'] = $_POST['txtusuario'];
  6. }
  7.  
  8.  
  9.  
  10. ?>
  11. <html>
  12.    <head>
  13.    </head>
  14.    <body>
  15.       <form action="form.php" method="post">
  16.          Escribe tu Nickname:
  17.          <input type="text" size="25" name="txtusuario" />
  18.          <br />
  19.          <input type="submit" value="Grabar" name="subir" />
  20. <?php
  21.  
  22. if (isset($_POST['subir'])) {
  23. echo "escribiste :".$_SESSION['nombre'];
  24. }
  25.  
  26. ?>
  27.       </form>
  28.    </body>
  29. </html>

¡Saludos!

Última edición por gonzaherrera77; 18/11/2015 a las 22:20
  #3 (permalink)  
Antiguo 19/11/2015, 07:45
Avatar de agusax  
Fecha de Ingreso: junio-2011
Ubicación: Puerto Real (Cádiz)
Mensajes: 74
Antigüedad: 12 años, 10 meses
Puntos: 3
Respuesta: porqué no funciona variable session

Me pasó algo similar, yo lo tengo así (un login simple)
LOGIN.php
Código PHP:
Ver original
  1. <HTML LANG="es">
  2.  
  3. <HEAD>
  4.     <TITLE>Login</TITLE>
  5.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6.     <LINK REL="stylesheet" TYPE="text/css" HREF="estilo.css">
  7.  
  8. </HEAD>
  9.  
  10. <?php
  11.     // Para que no se muestre warning
  12.     error_reporting(E_ERROR | E_PARSE);
  13.  
  14.     session_start();
  15.  
  16.     $_SESSION['u']= $_REQUEST['login'];
  17.     $_SESSION['p']= $_REQUEST['pass'];
  18.    
  19. //AQUI REALIZAS LA CONEXION A LA BD Y BUSCAS EL USUARIO Y PASS DE LA TABLA
  20.  
  21.     if (COMPRUEBAS QUE ES CORRECTO) {
  22.         header("Location: index.php");
  23.     }
  24. ?>
  25.  
  26. <BODY>
  27.  
  28.     <div id="title">
  29.         <center>
  30.             <h1>Login</h1>
  31.         </center>
  32.     </div>
  33.     <div id="formulario">
  34.         <form method='post' action='login.php'>
  35.             <center>
  36.                 Usuario: <br> <INPUT TYPE="text" NAME="login" SIZE="20" MAXLENGTH="20">
  37.                 <br>
  38.                 Contraseña: <br> <INPUT TYPE="password" NAME="pass" SIZE="20" MAXLENGTH="20">
  39.                 <br>
  40.                 <INPUT TYPE="SUBMIT" NAME="enviar" VALUE="Enviar">
  41.             </center>
  42.         </form>
  43.     </div>
  44.  
  45. </BODY>
  46. </HTML>

Espero que te sea de ayuda
  #4 (permalink)  
Antiguo 19/11/2015, 09:41
 
Fecha de Ingreso: noviembre-2010
Mensajes: 47
Antigüedad: 13 años, 5 meses
Puntos: 1
Respuesta: porqué no funciona variable session

Gracias por sus aportes ambos comentarios me funcionan muy bien, gracias por tomarse un poco de su tiempo para prestar suferencias y encontrar soluciones. Saludos
  #5 (permalink)  
Antiguo 19/11/2015, 13:46
 
Fecha de Ingreso: noviembre-2010
Mensajes: 47
Antigüedad: 13 años, 5 meses
Puntos: 1
Respuesta: porqué no funciona variable session

Aqui regresando, ya tengo un casi destrozadero de codigo y al parecer cuando hago un if para si selecciono un boton me haga ciertas acciones, entonces para ver que funcione dentro del if imprimo la variable de sesion y no me toma el dato, si la imprimo fuera del if si lo hace.

<?php
session_start(); //Iniciamos la sesion
if(isset($_POST['nomUsuario'])) {
$_SESSION['nombre'] = $_REQUEST['nomUsuario'];
}
?>
<!DOCTYPE HTML>
<HTML>
<head>
<meta charset="iso-8859-1">
<meta name="description" content="Pagina para descargas electrónicas">
<meta name="keywords" content="HTML5, CSS3, JavaScript">
<title>Modificaciones datos de usuario</title>
<link rel="stylesheet" href="../CSS/formatoBuscar.css">
<link rel="stylesheet" href="../CSS/contenedores.css">
<link rel="stylesheet" href="../CSS/formatoTablas.css">

<script type="text/javascript" language="javascript">


//var nom=document.forms.form1.nomUsuario.value;

//valor=document.form1.nomUsuario.value;
//location.href='qh.php?variable='+valor;


</script>


</head>
<body>
<div id="agrupar">
<header id="cabecera">
<div id="contenedor">
<div id="flotanteizquierda">
<img src="http://www.forosdelweb.com/f18/Imagenes/SEP.png" id="logo1" >
</div>
<div id="flotanteCentro">
<!-- <label id="texto1"> <h2> REPOSITORIO DE LIBROS, REVISTAS Y ARTÍCULOS ELECTRÓNICOS </h2></label> -->
<h2 id="titPrin"> REPOSITORIO DE LIBROS, REVISTAS Y ARTÍCULOS ELECTRÓNICOS </h2>
</div>
<div id="flotantederecha">
<img src="http://www.forosdelweb.com/f18/Imagenes/LogoTec.png" id="logo2" >
</div>
</div>

</header>

<div id="contMenu2">
<ul id="menu2">
<li><a href="">Revistas</a></li>
<li><a href="#">Articulos</a></li>
<li><a href="#">Libros</a></li>
<li><a href="#">Cerrar Sesión</a></li>

<!--<div id="lavalamp"></div> -->
</ul>
</div>

<section id="seccion2">
<article>


<div id="contModifUsr">
<div id="cambiaUsuario" >
<form name="form1" method="POST" action=" ">
<label class="etiNom"> Nombre Usuario :

</label>
<input type="text" name="nomUsuario" id="nomUsuario" class="etInput" />
<input type='submit' class='btnBuscUs' name='buscar' value="Busqueda"></input>

<table BORDER=1 CELLSPACING=1 CELLPADDING=1>
<tr style="background-color:yellow">
<td>Usuario</td>
<td>Nombre</td>
<td>Direccion</td>

</tr>

<?php
//echo "variable de form1 es :".$_SESSION['nombre'];

if (isset($_POST['buscar']))
{
$valor=$_POST['nomUsuario'];


include("../Conexion.php");
$link=conectarse();
$valor=$_POST['nomUsuario'];
$sql="SELECT NombreUsuario,Nombre,Direccion FROM Usuarios where NombreUsuario like '$valor%' ";

$consulta=mysql_query($sql,$link);

while($fila = mysql_fetch_array($consulta))
{


//printf("<tr><td>%s</td> <td>%s</td> <td>%s</td> <td>%d</td></tr>");
//echo "<tr><td>$fila[noControl]</td> <td>$fila[nombre]</td> <td>$fila[direccion]</td> <td>$fila[edad]</td> </tr>";
echo "<tr>";


//echo "<td><input type='text' name='caja1' value='$fila[0]' hidden='true'/></td>";
//echo "<td><input type='text' name='caja1' value='$fila[0]' readonly='true'/></td>";
echo "<td><input type='text' name='caja1' value='$fila[0]' disabled='true'/></td>";
echo "<td><input type='text' name='caja2' value='$fila[1]'></td>";
echo "<td><input type='text' name='caja3' value='$fila[2]'></td>";

echo "</tr>";
//echo "<br> e numero de control es ".'$_POST[cajaBuscar]';
//echo "<td><a href=\"eliminarRegistros2.php?id=$fila[noControl]\">Eliminar</a></td></tr>";
}
}

echo "</table>";
echo "<input type='submit' name='cambios' value='Guardar Cambios' />";
//<?php
$variable = $_SESSION['nombre'];
echo "variable inicial es ".$variable; //aqui si imprime valor

if (isset($_POST['cambios'])==true) {
//include("../Conexion.php");
//$link=conectarse();
echo "variable inicial es :".$variable; //Aqui ya no me trae el valor
//echo "Nuevo valor es : ".$_POST['caja2'];
//mysql_query("update Usuarios set nombre = '$_POST[caja1]', direccion = '$_POST[caja2]', edad = '$_POST[caja3]' where noControl = '$_POST[$valor]'",$link);


}

?>


</div>
</div>

</article>
</section>



</div>
</body>
</HTML>

Etiquetas: formulario, html, session, variable
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:29.