Logo Luan Morina
Store a radio button value in session (demo)
WHICH SEASON DO YOU PREFER?

What is PHP session?

A PHP session is a way to store information on the server to be used across multiple pages of a web application. Unlike cookies, which store data on the user’s computer, session data is stored on the server. This function must be called at the beginning of your script, before any other HTML tags. Here's a simple example with radio buttons:

HTML

<?php
session_start();
if (isset($_POST['season'])){
$_SESSION['season'] = $_POST['season'];
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Session</title>
</head>
<body>
<div>
<form method="POST" action="<?php echo $PHP_SELF;?>">
<span>What's your favourite season?</span>
<ul>
<li><input type="radio" name="season" value="spring" onClick="submit();" <?php echo ($_SESSION['season'] == "spring") ? 'checked' : ''; ?> />Spring</li>
<li><input type="radio" name="season" value="summer" onClick="submit();" <?php echo ($_SESSION['season'] == "summer") ? 'checked' : ''; ?> />Summer</li>
<li><input type="radio" name="season" value="autumn" onClick="submit();" <?php echo ($_SESSION['season'] == "autumn") ? 'checked' : ''; ?> />Autumn</li>
<li><input type="radio" name="season" value="winter" onClick="submit();" <?php echo ($_SESSION['season'] == "winter") ? 'checked' : ''; ?> />Winter</li>
</ul>
</form>
</div> 
<p>Your favourite season is: </p><?php echo $_SESSION['season'];?> <p>
</body>
</html>

   Store a radio button value in a session