Select Dynamically Add Options Using AngularJS With PHP/MySQLi | CampCodes

Select Dynamically Add Options using AngularJS with PHP/MySQLi

February 2, 2021
Select Dynamically Add Options using AngularJS with PHP/MySQLi

Tutorial: Select Dynamically add Options using AngularJS with PHP/MySQLi with Source Code

Getting Started on Select Dynamically Add Options using AngularJS

This is a tutorial on Select Dynamically Add Options using AngularJS with PHP/MySQLi. I’ve used CDN for Bootstrap and Angular JS in this tutorial, so you need an internet connection for them to work.

Creating our Database

First, we’re going to create our database.

1. Open phpMyAdmin.

2. Click databases, create a database and name it as angular.

3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.

CREATE TABLE `fruits` (
  `fruitid` int(11) NOT NULL AUTO_INCREMENT,
  `fruitname` varchar(30) NOT NULL,
PRIMARY KEY(`fruitid`)

Select Dynamically Add Options using AngularJS


This is a index that contains an add form, table and select input.

<!DOCTYPE html>
<html lang="en" ng-app="app">
    <meta charset="utf-8">
    <title>AngularJS Select - Dynamically add Options using PHP/MySQLi</title>
    <link href="" rel="stylesheet">
    <script src=""></script>
    <style type="text/css">
<body ng-controller="myController" ng-init="fetch()">
<div class="container">
    <h1 class="page-header text-center">AngularJS Select - Dynamically add Options using PHP/MySQLi</h1>
    <div class="col-md-2 col-md-offset-1">
    	<h3>Add new Fruit</h3>
    	<input type="text" placeholder="Fruit Name" class="form-control" ng-model="newFruit.fruitname"><br>
    	<button type="button" class="btn btn-primary" ng-click="add()"><span class="glyphicon glyphicon-floppy-disk"></span> Save</button>
    <div class="col-md-6">
        <h3>Fruit Table</h3>
        <table class="table table-bordered table-striped">
                    <th>Fruit ID</th>
                    <th>Fruit Name</th>
                <tr ng-repeat="fruit in fruits">
                    <td>{{ fruit.fruitid }}</td>
                    <td>{{ fruit.fruitname }}</td>
    <div class="col-md-2">
        <h3>Select Fruit</h3>
        <!-- <select ng-model="selectedFruit">
            <option ng-repeat="x in fruits" value="{{x.fruitid}}">{{x.fruitname}}</option>
        </select> -->
        <select ng-model="selectedFruit" ng-options="x.fruitname for x in fruits" class="form-control">
        <p><b>You selected:</b> {{selectedFruit.fruitname}}</p>
        <p><b>ID:</b> {{selectedFruit.fruitid}}</p>
        <!-- <p><b>You selected:</b> {{selectedFruit}}</p> -->
<script src="angular.js"></script>


This contains a angular js scripts.

var app = angular.module('app', []);
app.controller('myController', function($scope, $http){
    $scope.fetch = function(){
    	    $scope.fruits = data; 
    $scope.add = function(){
    	$"add.php", $scope.newFruit)
    		$scope.newFruit = '';


This is a PHP code/api that fetches data from a database.

    $conn = new mysqli('localhost', 'root', '', 'angular');
    $output = array();
    $sql = "SELECT * FROM fruits";
        $output[] = $row;
    echo json_encode($output);


This is a PHP code/api in adding data into a table.

    $conn = new mysqli('localhost', 'root', '', 'angular');
    $data = json_decode(file_get_contents("php://input"));
    $fruitname = $data->fruitname;
    $sql = "INSERT INTO fruits (fruitname) VALUES ('$fruitname')";

ng-options vs ng-repeat

In this tutorial, we have used ng-options. By doing so, the options in our select input are objects. Whereas, when using ng-repeat, the possibilities become a string.

I’ve added in the comments in index.html the code in using ng-repeat for input select. You can exchange between ng-options for you to spot the difference.

That ends this tutorial. Happy Coding!

Related Tutorials: Save Selected Options using AngularJS and PHP/MySQLi, Load More Data using AngularJS with PHP/MySQLi, Add Form Fields Dynamically using AngularJS with PHP/MySQLi

Download Here

Leave a Reply

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