Skip to main content

AngularJs Crud Insert Data

<?php
$server="localhost";
$user="root";
$pass="iq123";
$db="test";
$conn= mysqli_connect($server,$user,$pass,$db);
if (!$conn){
echo "mysqli_error().'Database not connected'";
}
?>



<?php
include("conn.php");
$data=json_decode(file_get_contents("php://input"));
$name=$data->name;
$price=$data->price;
$quantity=$data->quantity;

$sql="Insert into product(name,price,quantity)values('$name','$price','$quantity')";
$query=mysqli_query($conn,$sql);
?>







<!DOCTYPE>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>InsertData</title>
<script src="js/angular.js" language="javascript" type="text/javascript"></script>
<script src="js/angular.min.js" language="javascript" type="text/javascript"></script>
<link href="css/bootstrap.css" rel="stylesheet" type="text/css"/>
</head>

<body ng-app='myapp' ng-controller="productController">
<h2>CRUD with AngularJS</h2>
<table class='table table-hover'>
<tr>
<th>Id </th>
<th>Name </th>
<th>Price </th>
<th>Quantity </th>
<th>option </th>
</tr>
<tr ng-repeat="product in listproduct">
<td>{{product.id}}</td>
<td>{{product.name}}</td>
<td>{{product.price}}</td>
<td>{{product.quantity}}</td>
<td>
<a class='btn btn-danger' href="#" ng-click="del(product.id)">Delete</a> |
<a  class='btn btn-info' href="#" ng-click="selectEdit(product.id)">Edit</a>
</td>
</tr>

</table>
<h3>product Information </h3>
<table class='table table-bordered'>
<tr>
<td>Id</td>
<td><input type='text' class='form-control' ng-model='id'></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" class='form-control' ng-model="name"></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" class='form-control' ng-model='price'></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type='text' class='form-control'  ng-model='quantity'></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type='button' class='btn btn-primary' value="Add" ng-click="add()">
<input type='button' class='btn btn-info' value="Save" ng-click="edit()">
<input type='button' class='btn btn-success' value="Insert" ng-click="insert()">
</td>
</tr>



</table>
<script>
var myapp = angular.module('myapp', []);
myapp.controller('productController',function($scope,$http){
$scope.listproduct = [
{id: 'p01',name:'Mobile',price:10000, quantity:20},
{id: 'p02',name:'Fridge',price:50000, quantity:20},
{id: 'p03',name:'TV',price:45000, quantity:20},
{id: 'p04',name:'Desktop',price:12500, quantity:20},
{id: 'p05',name:'Air Contion',price:75000, quantity:20},
];

$scope.add = function(){
$scope.listproduct.push({
id:$scope.id, name:$scope.name, price:$scope.price, quantity:$scope.quantity
});
$scope.id = '';
$scope.name = '';
$scope.price = '';
$scope.quantity = '';
};

$scope.insert = function(){
$http.post("insertData.php",
{'name':$scope.name,
'price':$scope.price,
'quantity':$scope.quantity
})

.success(function(data,status,headers,config){
alert("Data Isnserted Successfully");
//window.location.href = 'viewPage.php';
})

};




$scope.edit = function(){
var index = getSelectIndex($scope.id);
$scope.listproduct[index].name = $scope.name;
$scope.listproduct[index].price = $scope.price;
$scope.listproduct[index].quantity = $scope.quantity;

};


$scope.selectEdit = function(id){
var index = getSelectIndex(id);
var product = $scope.listproduct[index];
$scope.id = product.id;
$scope.name = product.name;
$scope.price = product.price;
$scope.quantity = product.quantity;
};

$scope.del = function(id){
var result = confirm('Are you sure?');
var index =  getSelectIndex(id);
$scope.listproduct.splice(index, 1);

};
function getSelectIndex(id){
for(var i=0;i<$scope.listproduct.length; i++)
if($scope.listproduct[i].id==id)
return i;
return -1;

}
});

</script>
</body>
</html>

Comments

Popular posts from this blog

IDB HTML MCQ

Questions: 1. A webpage displays a picture. What tag was used to display that picture? a. picture b. image c. img d. src 2. <b> tag makes the enclosed text bold. What is other tag to make text bold? a. <strong> b. <dar> c. <black> d. <emp> 3. Tags and test that are not directly displayed on the page are written in _____ section. a. <html> b. <head> c. <title> d. <body> 4. Which tag inserts a line horizontally on your web page? a. <hr> b. <line> c. <line direction=”horizontal”> d. <tr> 5. What should be the first tag in any HTML document? a. <head> b. <title> c. <html> d. <document> 6. Which tag allows you to add a row in a table? a. <td> and </td> b. <cr> and </cr> c. <th> and </th> d. <tr> and </tr> 7. How can you make a bulleted list? a. <list> b. <nl> c. <ul> d. <ol> 8. Ho

Database Connection with PHP PDO MYSQL

PDO Connection MYSQLI // Create Connection with Pdo\ $dsn = "mysql:host=localhost; dbname=test;"; $db_user = "root"; $db_password = ""; try{     $conn = new PDO("$dsn", "$db_user","$db_password");    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);     // $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);     echo "Connected"; }catch(PDOException $a){     echo "Connection Failed". $a->getMessage(); }