Angular JS developer USA

AngularJS is a front-end javascript framework and PHP a programming language to make dynamic and database driven websites.

AngularJS With PHP is very powerful when it comes to interactive HTML5 and model binding. On the other hand, PHP enables quick, well-structured, safe and powerful web application development. Both technologies provide sophisticated means for data access, iteration and page layouting.

AngularJS page can communicate with the server via REST services to get data instead of retrieving it from for example PHP variables directly.

In AngularJS With PHP, we use the $http service that makes working Like AJAX.

In this Blog we create simple demo for post form data on php serve and get data using AngularJS With PHP.

we are going to develop a book list application that can store book's name, book's price and book's created date. The application enables us to add, read and delete the records. The server side is written in PHP.

First we crete html form as below to submit and save to database.

HTML Page – Index

<!doctype html>
<html ng-app="phpApp">
<head lang="en">
<meta charset="utf-8">
<title>Angular js post data on php server</title>
<h2>The form</h2>
<div ng-controller="bookController"
	<p>Add Book</p>
	<label>Book Name</label>
	<input type="text" ng-model="bookName">
	<label>Book Price</label>
	<input type="text" ng-model="bookPrice">
	<label>Book Author</label>
	<input type="text" ng-model="bookAuthor">
    <input type="button" value="Add Book" ng-click="addBook()">
    <h2>The Book list</h2>
      <li ng-repeat="books in book">
      {{ book.book_name }} | {{ book.book_price}} |  {{ book.book_author }}
<script src="/libs/angular-1.4.9.min.js"></script>
<script src="js/app.js"></script>

In above code we include 2 js files; one is Angularjs library and another one app.js which we will be using in writing controller of our form.

On First part We define the application by using ng-app on the html tag. The name of the application is phpApp.

We define Controller of our form on div which wrapper form tags is ng- controller =bookController.

On submit button we put ng-click = addBook() on clicking on this button submit book data.

On second part of above code display book list. In this ng-repeat display all record of book.

Now we, create app.js file in js directory


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

      $scope.getBook = function() {
        success(function(response) {
        $scope.books = response;
	 $scope.addBook = function() {
         $$scope.url, {"book_name": $scope.bookName, "book_price": $scope.bookPrice, "book_author": $scope.bookAuthor}).
         success(function(response) {

In above code First we define module application as “phpApp”.

After this we define our controller 'bookController' with two dependency '$scope' and '$http'.

For current scope we use $scope.

$http is service is what we use in angular applications to perform Ajax calls.

Next, we define addBook function and post data on url define in $scope.url And in there success call getbook function and get all book record.

Now we create database table for books Like this

  `book_name` varchar(60) DEFAULT NULL,
  `book_price` float(8,2) DEFAULT NULL,
  `book_author` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)

Next we create dbconnect.php file for connectivity with database like this, dbconnect.php

define('DB_HOST', 'localhost');
function connect()
  $connect = mysqli_connect(DB_HOST ,DB_USER ,DB_PASS ,DB_NAME);
  if (mysqli_connect_errno($connect))
    die("Failed to connect:" . mysqli_connect_error());
  mysqli_set_charset($connect, "utf8");
  return $connect;

Next we create post.php file insert book record in database


require 'dbconnect.php';

$connect = connect();

$bookdata = file_get_contents("php://input");
if(isset($bookdata) && !empty($bookdata))
    $request     = json_decode($bookdata);
    $bookname  = $request->book_name;
    $bookprice = $request->book_price;
    $bookauthor = $request->book_author;
  $sql = "INSERT INTO `books`('book_name', 'book_price,'book_author') VALUES ('$bookname','$bookprice','$bookauthor')";


In above code we get posted form data in the following code file_get_contents("php://input");

The data need to json decoded as we seen above code.

At last we create get.php file for fetch all book records data in json format.

require 'dbconnect.php';
$connect = connect();
// Get the data
$book = array();
$sql = "SELECT * FROM books";
if($result = mysqli_query($connect,$sql))
  $count = mysqli_num_rows($result);
  $i = 0;
  while($row = mysqli_fetch_assoc($result))
      $book[$i]['id']    = $row['id'];
      $book[$i]['book_name']  = $row['book_name'];
      $book[$i]['book_price'] = $row['book_price'];
      $book[$i]['book_author'] = $row['book_author'];
$json = json_encode($book);
echo $json;

In above code we pass data as json_encode() format.