Storing data from Checkboxes in a PHP Session
Hello everyone!
This is a simple example for storing values from a form in a PHP session and displaying the values stored in the session.
The HTML page(session_check.html) has a form with the fields as shown in the image below:

session_check.html
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
</head>
<body>
<form action=”session_check2.php” method=”GET”>
user id <input type=”text” name=”user_id” /><br />
events<input type=”checkbox” value=”events” name=”check1″ /><br />
seminars<input type=”checkbox” value=”seminars” name=”check2″ /> <br />
workshop<input type=”checkbox” value=”workshop” name=”check3″ /><br />
<input type=”submit” />
</form>
</body>
</html>
The PHP script below stores the user id and checked options in the session and show the results.
session_check2.php
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
</head>
<?php
if(isset($_GET[‘user_id’])){
session_start();
$user = $_GET[‘user_id’];
$_SESSION[‘user’] = $user;
if(isset($_GET[‘check1’])){
$event = $_GET[‘check1’];
$_SESSION[‘event’] = $event;
}
if( isset($_GET[‘check2’])){
$seminar = $_GET[‘check2’];
$_SESSION[‘seminar’] = $seminar;
}
if(isset($_GET[‘check3’])) {
$workshop = $_GET[‘check3’];
$_SESSION[‘workshop’] = $workshop;
}
}
echo “user id: “.$_SESSION[‘user’];
if(isset($_SESSION[‘event’])){
echo “<br>event: “. $_SESSION[‘event’] ;
}
if( isset($_SESSION[‘seminar’])){
echo “<br>seminar: “. $_SESSION[‘seminar’] ;
}
if(isset($_SESSION[‘workshop’])) {
echo “<br>workshop: “.$_SESSION[‘workshop’];
}
session_destroy();
?>
<body>
</body>
</html>
here is the output:

Try this code. Copy and paste and run the code to see the result.
Keep posting your queries and suggestions!
Thank you..
Code On! 😀




