Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing Form Data as a Session Variable

Tags:

So I was wondering if it would be possible to store data coming in from a form as a session variable.

Heres what I have so far, but I don't know what to put for the Form Action.

Thanks for looking!

<strong>Test Form</strong>
<form action="" method"post">
    <input type="text" name="picturenum"/>
    <input type="submit" name="Submit" value="Submit!" />
</form>

<? 
    if (isset($_POST['Submit'])) { 
        $_session['picturenum'] = $_POST['picturenum'];
    } 
?> 

<strong><? echo $_session['picturenum'];?></strong>
like image 628
Kevin Johnson Avatar asked Sep 24 '10 22:09

Kevin Johnson


People also ask

What do you use to store data in a session variable?

To use session variables, it's necessary to start the session by using the session_start function, this will allow you to store your data in the global variable $_SESSION in a productive way.

How do you store values in a session?

The Session Storage basically consists of 4 main methods. setItem(key, value): This method is used to set the value into the Session Storage based on the key. getItem(key): This method is used to get the value that is stored into the Session Storage. It takes a key and returns the value.


2 Answers

To use session variables, it's necessary to start the session by using the session_start function, this will allow you to store your data in the global variable $_SESSION in a productive way.

so your code will finally look like this :

<strong>Test Form</strong>
<form action="" method"post">
<input type="text" name="picturenum"/>
<input type="submit" name="Submit" value="Submit!" />
</form>

<?php 
 
 // starting the session
 session_start();


 if (isset($_POST['Submit'])) { 
 $_SESSION['picturenum'] = $_POST['picturenum'];
 } 
?> 

<strong><?php echo $_SESSION['picturenum'];?></strong>

to make it easy to use and to avoid forgetting it again, you can create a session_file.php which you will want to be included in all your codes and will start the session for you:

session_start.php

 <?php
   session_start();
 ?> 

and then include it wherever you like :

<strong>Test Form</strong>
<form action="" method"post">
<input type="text" name="picturenum"/>
<input type="submit" name="Submit" value="Submit!" />
</form>

<?php 
 
 // including the session file
 require_once("session_start.php");


 if (isset($_POST['Submit'])) { 
 $_SESSION['picturenum'] = $_POST['picturenum'];
 } 
?> 

that way it is more portable and easy to maintain in the future.

other remarks

  • if you are using Apache version 2 or newer, be careful. instead of
    <?
    to open php's tags, use <?php, otherwise your code will not be interpreted

  • variables names in php are case-sensitive, instead of write $_session, write $_SESSION in capital letters

good work!

like image 87
Fopa Léon Constantin Avatar answered Sep 21 '22 11:09

Fopa Léon Constantin


That's perfectly fine and will work. But to use sessions you have to put session_start(); on the first line of the php code. So basically

<?php
session_start();

//rest of stuff

?>
like image 41
kjones1876 Avatar answered Sep 21 '22 11:09

kjones1876