Submit Multiple Form Using PHP

In this tutorial we will create a Submit Multiple Form using PHP. The code itself will send a multiple form data to the database server. This is a user-friendly kind of program feel free to modify it. To learn more about this tutorial, just follow the step below.

Getting 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 bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_multiple_input after that click Import then locate the database file inside the folder of the application then click ok. tut

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php
  1. <?php
  2.         $conn=mysqli_connect('localhost', 'root', '', 'db_multiple_input');
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>

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 your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="comtainer-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">Submit Multiple Form Using PHP</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-8">
  18.                         <div class="table-responsive">
  19.                         <table class="table table-bordered">
  20.                                 <thead class='alert-info'>
  21.                                         <tr>
  22.                                                 <th>Leader Name</th>
  23.                                                 <th>Member's Name</th>
  24.                                         </tr>
  25.                                 </thead>
  26.                                 <tbody>
  27.                                         <?php
  28.                                                 require'conn.php';
  29.                                                
  30.                                                 $query=mysqli_query($conn, 'SELECT * FROM `member`') or die(mysqli_error());
  31.                                                 while($fetch=mysqli_fetch_array($query)){
  32.                                         ?>
  33.                                         <tr>
  34.                                                 <td><?php echo $fetch['leader']?></td>
  35.                                                 <td><?php echo $fetch['member']?></td>
  36.                                         </tr>
  37.                                         <?php
  38.                                                 }
  39.                                         ?>
  40.                                 </tbody>
  41.                         </table>
  42.                 </div> 
  43.                 </div>
  44.                
  45.                 <div class="col-md-4">
  46.                         <form method="POST" action="save.php">
  47.                                 <div class="form-group">
  48.                                         <label>Leader Name</label>
  49.                                         <input type="text" name="leader" placeholder="Enter here..." class="form-control" required="required"/>
  50.                                 </div>
  51.                                 <br />
  52.                                 <label>Member's Name</label> <button type="button" class="btn btn-success btn-sm" onclick="addEntry();"><span class="glyphicon glyphicon-plus"></span></button>
  53.                                 <br /><br />
  54.                                 <div id="member">
  55.                                         <div class="form-group">
  56.                                                 <input type="text" name="member[]" placeholder="Enter here..." class="form-control" required="required"/>
  57.                                         </div>
  58.                                 </div>
  59.                                 <center><button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Submit</button></center>
  60.                         </form>
  61.                 </div>
  62.         </div>
  63. <script src="js/script.js"></script>
  64. </body>
  65. </html>

Creating the Main Function

This code contains the main function of the application. This code can store multiple entry to database. To make this just copy and write these block of codes below inside the text editor, then save it as save.php
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(ISSET($_POST['save'])){
  5.                 $parent = $_POST['leader'];
  6.                 $member = $_POST['member'];
  7.                
  8.                 $members=implode(', ', $member);
  9.                
  10.                 mysqli_query($conn, "INSERT INTO `member` VALUES('', '$parent', '$members')") or die(mysqli_error());
  11.                
  12.                
  13.                 header("location: index.php");
  14.         }
  15. ?>
There you have it we successfully created Submit Multiple Form 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!

Add new comment