You can store single value to session in PHP easily. Visit store values to session in php.
where,
session_start() - start the session
$ar=array() - array variable
Now you can get output like this:
php
mysql
jquery
<html>
<body>
<?php
session_start();
error_reporting('E_ALL ^ E_NOTICE');
if($_POST['submit'])
{
if(count($_SESSION['arr'])==0)
{
$ar=array();
$val=$_POST['value'];
array_push($ar,$val);
$_SESSION['arr']=$ar;
print_R($_SESSION['arr']);
}
else
{
$val=$_POST['value'];
array_push($_SESSION['arr'],$val);
print_R($_SESSION['arr']);
}
}
?>
<form action="#" method="post">
<input type="text" name="value">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
The above example describes array values are storing into session in PHP while form submitting.
where,
if(count($_SESSION['arr'])==0) {} - check whether the array contains values or not. Create array variable and store value into array, then value is stored into session if there is no values in session. Otherwise it directly add values into session.
array_push() - add values to array in PHP.
Related Post:
Store array values to session in PHP:
You can also store multiple values into session using PHP. You can store one or more than one values in array that can be stored into session. Consider the following example. It describes the array values directly stored into session in PHP. The following PHP script is used to store values to session in PHP.
<?php
session_start();
$ar=array('php','mysql','jquery');
$_SESSION['arr']=$ar;
$session=$_SESSION['arr'];
foreach($session as $val)
{
echo $val.'<br>';
}
?>
session_start();
$ar=array('php','mysql','jquery');
$_SESSION['arr']=$ar;
$session=$_SESSION['arr'];
foreach($session as $val)
{
echo $val.'<br>';
}
?>
where,
session_start() - start the session
$ar=array() - array variable
Now you can get output like this:
php
mysql
jquery
Store multiple (array) values into session in PHP on form submit:
Consider the following example:
<html>
<body>
<?php
session_start();
error_reporting('E_ALL ^ E_NOTICE');
if($_POST['submit'])
{
if(count($_SESSION['arr'])==0)
{
$ar=array();
$val=$_POST['value'];
array_push($ar,$val);
$_SESSION['arr']=$ar;
print_R($_SESSION['arr']);
}
else
{
$val=$_POST['value'];
array_push($_SESSION['arr'],$val);
print_R($_SESSION['arr']);
}
}
?>
<form action="#" method="post">
<input type="text" name="value">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
The above example describes array values are storing into session in PHP while form submitting.
where,
if(count($_SESSION['arr'])==0) {} - check whether the array contains values or not. Create array variable and store value into array, then value is stored into session if there is no values in session. Otherwise it directly add values into session.
array_push() - add values to array in PHP.
Related Post: