Multiple Form Inputs In PHP | CampCodes

Multiple Form Inputs in PHP

June 7, 2020
multiple forms

In this tutorial, we will create a Multiple Form Inputs using PHP. This code will allow the user to insert multiples form data to the database server when the user clicks the submit button. The code use MySQLi INSERT query to store data entry to the MySQLi server; after submitted, the PHP script will compress the input array by using implode to store the data to the database in one submission. This a user-friendly program; feel free to modify and use it to your system.

We will be using PHP as a scripting language that interprets in the webserver such as xamp, wamp, etc. It is widely used by modern website application to handle and protect user confidential information.

Table of Contents

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server

And, this is the link for the bootstrap that I used for the layout design

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.

multiple form inputs in php

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.

    $conn=mysqli_connect('localhost', 'root', '', 'db_multiple_input');
        die("Error: Failed to connect to database!");

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.

<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
 		<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    <nav class="navbar navbar-default">
        <div class="comtainer-fluid">
            <a class="navbar-brand" href="">Sourcecodester</a>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Multiple Form Inputs</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-4">
            <form method="POST" action="save.php">
                <div class="form-group">
                    <label>Parent Name</label>
                    <input type="text" name="parent" placeholder="Enter here..." class="form-control" required="required"/>
                <br />
                <label>Children's Name</label> <button type="button" class="btn btn-success btn-sm" onclick="addEntry();"><span class="glyphicon glyphicon-plus"></span></button>
                <br /><br />
                <div id="children">
                    <div class="form-group">
                        <input type="text" name="children[]" placeholder="Enter here..." class="form-control" required="required"/>
                <center><button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Submit</button></center>
        <div class="col-md-8">
            <div class="table-responsive">
                <table class="table table-bordered">
                    <thead class='alert-info'>
                            <th>Parent Name</th>
                            <th>Children's Name</th>
                            $query=mysqli_query($conn, 'SELECT * FROM `member`') or die(mysqli_error());
                            <td><?php echo $fetch['parent']?></td>
                            <td><?php echo $fetch['children']?></td>



Creating the Main Function

This code contains the main function of the application. This code will insert multiple form input when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as save.php.

    require_once 'conn.php';
        $parent = $_POST['parent'];
        $children = $_POST['children'];
        $childrens=implode(', ', $children);
        mysqli_query($conn, "INSERT INTO `member` VALUES('', '$parent', '$childrens')") or die(mysqli_error());
        header("location: index.php");

Note: To make this code work make sure you save this file inside the js directory.

function addEntry(){
    var entry="<input type='text' name='children[]' placeholder='Enter here...' class='form-control' required='required'/>";
    var element=document.createElement("div");
    element.setAttribute('class', 'form-group');

Leave a Reply

Your email address will not be published. Required fields are marked *