AngularJS - Add Remove Input Fields Dynamically with PHP MySQLi

By Hardik Savani | January 14, 2018 | | 19565 Viewer | Category : PHP Bootstrap MySql AngularJS

today i would like to share with you how to implement add more add and remove textbox dynamically using angularjs with PHP. you can easily adding form fields dynamically and save in mysql database.

If you require to add input fields dynamically in angular js using php mysql database then you are right place. here you will find full script for add remove input fields dynamically in angularjs and you can save it to mysql database. you have to just create following files.

1) index.php

2) main.js

3) get_data.php

4) add_data.php

So just follow bellow tutorial and get dynamically add and remove form fields in angularjs. you just follow bellow two files and you will get full example like as bellow screen shot.


Create Index.php File:

here i will create index.php i will write bootstrap code and write design code. So le's copy bellow code.


<!DOCTYPE html>

<html lang="en" ng-app="liveApp">


<meta charset="utf-8">

<title>AngularJS - Add Remove Input Fields Dynamically with PHP MySQLi </title>

<!-- bootstrap.min.css -->

<link href="" rel="stylesheet">

<!-- angular.min.js -->

<script src=""></script>


<body ng-controller="studentData" ng-init="retrive()">

<div class="container">

<h1 class="page-header text-left">AngularJS - Add Remove Input Fields Dynamically with PHP MySQLi </h1>

<div class="row">

<div class="col-md-8 col-md-offset-2">

<!-- Success Message Display -->

<div class="alert alert-success text-left" ng-show="success">

<button type="button" class="close" ng-click="msgCls()"><span aria-hidden="true">×</span></button>

<i class="fa fa-check"></i> {{ messageSuccess }}


<!-- Error Message Display -->

<div class="alert alert-danger text-left" ng-show="error">

<button type="button" class="close" ng-click="msgCls()"><span aria-hidden="true">×</span></button>

<i class="fa fa-warning"></i> {{ msgError }}


<!-- Form Display -->

<form name="liveForm" novalidate>

<fieldset ng-repeat="item in itemList">

<div class="panel panel-default">

<div class="panel-body">

<div class="row">

<div class="col-md-5">

<input type="text" placeholder="Name" class="form-control" ng-model="" required>


<div class="col-md-5">

<input type="text" placeholder="Price" class="form-control" ng-model="item.price" required>


<button class="btn btn-danger btn-sm" ng-show="$last" ng-click="deleteField()"><span class="glyphicon glyphicon-remove"></span></button>





<button type="button" class="btn btn-success" ng-click="addFields()"><span class="glyphicon glyphicon-plus"></span> Add</button>

<button type="button" class="btn btn-success" ng-disabled="liveForm.$invalid" ng-click="liveFormSubmit()"><span class="glyphicon glyphicon-floppy-disk"></span> Save</button>


<!-- Table data display -->

<table class="table table-bordered live24u" style="margin-top:11px;">








<tr ng-repeat="stud in items">

<td>{{ }}</td>

<td>{{ stud.price }}</td>








<script src="main.js"></script>


Create main.js File:

Here we will write code for angular js and manage add more input fields. here write code for call api and get all data from tables. so let's create bellow file:


var liveApp = angular.module('liveApp', []);

liveApp.controller('studentData', function($scope, $http){

$scope.success = false;

$scope.error = false;

$scope.retrive = function(){


$scope.items = data;



$scope.itemList = [{id: 'firstField'}];

$scope.addFields = function(){

var addnewStudent = $scope.itemList.length+1;



$scope.liveFormSubmit = function(){

$'add_data.php', $scope.itemList)



$scope.error = true;

$scope.success = false;

$scope.msgError = data.comments;



$scope.error = false;

$scope.success = true;

$scope.messageSuccess = data.comments;


$scope.itemList = [{id: 'firstField'}];




$scope.deleteField = function() {

var itemLast = $scope.itemList.length-1;



$scope.msgCls = function(){

$scope.success = false;

$scope.error = false;



Create get_data.php File:

in this file we will write code for getting data from php mysql database. so let's create get_data.php on your root directory.



$db_con = new mysqli('localhost', 'root', 'root', 'laravel_test');

$lstoutput = array();

$msqsql = "SELECT * FROM items";



$lstoutput[] = $row;


echo json_encode($lstoutput);


Create add_data.php File:

in this file we will write code for save data from php mysql database. so let's create add_data.php on your root directory.



$db_con = new mysqli('localhost', 'root', 'root', 'laravel_test');

$alldata = json_decode(file_get_contents("php://input"));

$count = count($alldata);

$output_res = array('error' => false);

foreach($alldata as $key => $value){

$studfname = $value->name;

$studlname = $value->price;

$msqsql = "INSERT INTO items (name, price) VALUES ('$studfname', '$studlname')";

$mtquery = $db_con->query($msqsql);



$output_res['comments'] = "($count) Items added successfully";


$output_res['error'] = true;

$output_res['comments'] = "Cannot add Items";


echo json_encode($output_res);


Now you are ready to run full example of add more dynamic fields in angularjs using php script.

i hope it can help you...

***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: