Pages

Friday, 7 September 2018

Insert data using Angular and php

Index.php


<html ng-app="myapp">
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
  </head>
  <body style="background: gray;"><br><br>
    <div class="container" style="width:500px;">
          <h3 align="center" style="color: white;">AngularJS Tutorial With PHP</h3>
          <div ng-controller="userctrl" ng-init="displayData()">
               <label style="color: white;">Name</label>
              <input type="text" ng-model="nm" class="form-control">
              {{nm}}
              <br>
              <label style="color: white;">Email</label>
              <input type="test" ng-model="email" class="form-control">
              {{email}}
              <br>
              <label style="color: white;">Country</label>
              <input type="text" ng-model="country" class="form-control">
              {{country}}
              <br>
              <input type="submit" name="btnInsert" class="btn btn-info" ng-click="insertData()" value="Insert">
              <br><br>
              <table class="table table-bordered">
                  <tr>
                    <th>S.no</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Country</th>
                    <th>Edit</th>
                    <th>Delete</th>
                  </tr>
                  <tr ng-repeat="x in names">
                    <td> {{x.id}} </td>
                    <td> {{x.name}} </td>
                    <td> {{x.email}} </td>
                    <td> {{x.country}} </td>
                    <td> <button ng-click="updateData(x.id, x.name, x.email, x.country)" class="btn btn-info btn-xs">Update</button> </td>
                    <td> <button ng-click="deleteData(x.id)" class="btn btn-danger btn-xs">Delete</button> </td>
                  </tr>
              </table>
          </div>
    </div>
  </body>
</html>
<script>
  var app = angular.module("myapp",[]);
  app.controller("userctrl", function($scope, $http){
      $scope.insertData = function(){
        $http.post(
          "insert.php",
          {'nm':$scope.nm, 'email':$scope.email, 'country':$scope.country}
        ).success(function(data){
          alert(data);
          $scope.nm = null;
          $scope.email = null;
          $scope.country = null;
          $scope.displayData();
        });
      }

      $scope.displayData = function(){ 
      $http.get("display.php") 
           .success(function(data){ 
                $scope.names = data; 
           });
       }

     
  });

</script>


Insert.php


<?php

$con = mysqli_connect("localhost", "root", "", "test");
$data = json_decode(file_get_contents("php://input"));

if (count($data) > 0) {

$nm = mysqli_real_escape_string($con, $data->nm);
$email = mysqli_real_escape_string($con, $data->email);
$country = mysqli_real_escape_string($con, $data->country);

$query = "insert into crud values(null,'$nm','$email','$country')";
if (mysqli_query($con, $query)) {

echo "Data Inserted";
}else{

echo "Error";
}
}

?>



Display.php


<?php  

 $con = mysqli_connect("localhost", "root", "", "test");
 $output = array();  
 $query = "SELECT * FROM crud";  
 $result = mysqli_query($con, $query);  
 if(mysqli_num_rows($result) > 0)  
 {  
      while($row = mysqli_fetch_array($result))  
      {  
           $output[] = $row;  
      }  
 
      echo json_encode($output);  
 }  

 ?>