I've created a form that allows users to add and remove multiple fields, ranging from one to three.
My goal is to retrieve the form data and store it in an array.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="02606d6d76717670637242372c322c30">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<section>
<div class="container">
<form method="post" action="submit.php">
<div class="form-group fieldGroup">
<div class="input-group">
<div class="row">
<div class="col-lg-4">
<input type="text" name="userfname" class="form-control" placeholder="Enter First Name"/>
</div>
<div class="col-lg-4">
<input type="text" name="usermname" class="form-control" placeholder="Enter Middle Name"></input></div>
<div class="col-lg-4">
<input type="text" name="userlname" class="form-control" placeholder="Enter Last Name"></input></div>
</div>
<div class="row">
<div class="col-lg-4">
<input type="text" name="usermobilenumber" class="form-control" placeholder="Enter Mobile Number"></input></div>
<div class="col-lg-4">
<input type="text" name="userdob" class="form-control" placeholder="Enter Date of birth"></input></div>
<div class="col-lg-4">
<select id="gender">
<option value="volvo">Male</option>
<option value="saab">Female</option>
</select>
</div>
</div>
<div class="row">
<div class="col-lg-4">
<input type="text" name="useremail" class="form-control" placeholder="Enter Email id.."></input></div>
<div class="col-lg-4">
<input type="text" name="userpassword" class="form-control" placeholder="password"..></input></div>
<div class="col-lg-4">
<select id="User Category">
<option value="">Head of Family</option>
<option value="">Mother</option>
<option value="">Father</option>
<option value="">Brother</option>
<option value="">Sister</option>
<option value="">Dependent</option>
<option value="">Child</option>
<option value="">Spouse</option>
</select>
</div>
</div>
<div class="row">
<div class="input-group-addon">
<a href="javascript:void(0)" class="btn btn-success addMore"><span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span> Add</a>
</div>
</div>
</div>
</div>
<input type="submit" name="submit" class="btn btn-primary" value="SUBMIT"></input>
</form>
<!-- copy of input fields group -->
<div style="display:none;">Sample code for input fields here </div>
</div>
</section>
<script src="js/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href='/cdn-cgi/l/email-protection' class='__cf_email__' data-cfemail='abc9c4c4dfd8dfd9cadbeb9e859b8599'>[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
index.js
$(document).ready(function(){
//group add limit
var maxGroup = 15;
//add more fields group
$(".addMore").click(function(){
if($('body').find('.fieldGroup').length < maxGroup){
var fieldHTML = '<div class="form-group fieldGroup">'+$(".fieldGroupCopy").html()+'</div>';
$('body').find('.fieldGroup:last').after(fieldHTML);
}else{
alert('Maximum '+maxGroup+' groups are allowed.');
}
});
//remove fields group
$("body").on("click",".remove",function(){
$(this).parents(".fieldGroup").remove();
});
});
I am aiming to gather all the form values into an array-like structure as shown below:
[{'name': 'InputName', 'email': 'InputEmail'}, {'name': 'InputName', 'email': 'InputEmail'}]
I have designed two arrays within JSON format to accommodate my intended data based on the number of forms added.