PHP - How To Add Session Array
Submitted by razormist on Monday, August 6, 2018 - 12:58.
In this tutorial we will create a How To Add Session Array using PHP. This code can be use for storing of list of data. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...
There you have it we successfully created How To Add Session Array using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!
Before we get started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as index.php.- <!DOCTYPE html>
- <?php
- ?>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-wdith, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - How To Add Session Array</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <button class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Data</button>
- <br /><br />
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>Address</th>
- </tr>
- </thead>
- <tbody style="background-color:#fff;">
- <?php
- foreach($_SESSION['member'] as $key => $value){
- ?>
- <tr>
- <td><?php echo $value['firstname']?></td>
- <td><?php echo $value['lastname']?></td>
- <td><?php echo $value['address']?></td>
- </tr>
- <?php
- }
- }
- ?>
- </tbody>
- </table>
- </div>
- <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
- <div class="modal-dialog" role="document">
- <form action="save.php" method="POST">
- <div class="modal-content">
- <div class="modal-body">
- <div class="col-md-2"></div>
- <div class="col-md-8">
- <div class="form-group">
- <label>Firstname</label>
- <input class="form-control" type="text" name="firstname" required="required" />
- </div>
- <div class="form-group">
- <label>Lastname</label>
- <input class="form-control" type="text" name="lastname" required="required" />
- </div>
- <div class="form-group">
- <label>Address</label>
- <input class="form-control" type="text" name="address" required="required" />
- </div>
- </div>
- </div>
- <div style="clear:both;"></div>
- <div class="modal-footer">
- <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
- <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
- </div>
- </div>
- </form>
- </div>
- </div>
- </body>
- <script src="js/jquery-3.2.1.min.js"></script>
- <script src="js/bootstrap.js"></script>
- </html>
Creating the Main Function
This code contains the main function of the application. This code will store an array of data to session array. To do this just kindly copy and write these block of codes inside the text editor, then save it as save.php- <?php
- 'firstname' => $_POST['firstname'],
- 'lastname' => $_POST['lastname'],
- 'address' => $_POST['address']
- );
- $_SESSION['member'][$id] = $member;
- echo "<script>alert('Data Inserted')</script>";
- echo "<script>window.location='index.php'</script>";
- }else{
- 'firstname' => $_POST['firstname'],
- 'lastname' => $_POST['lastname'],
- 'address' => $_POST['address']
- );
- $_SESSION['member'][0] = $member;
- echo "<script>alert('Data Inserted')</script>";
- echo "<script>window.location='index.php'</script>";
- }
- }
- ?>
Add new comment
- 16 views