AngularJs Data Insert PHP-MySQL


ng-ajax.php
<!DOCTYPE html>
<html>
<head>
<title>Angular Ajax</title>
<!DOCTYPE html>
<html ng-app="app">
<head>
<title>AngularJs</title>
<script type="text/javascript" src="angular-1.5.5/angular.js"></script>
<script type="text/javascript" src="angular-1.5.5/angular-route.js"></script>
<script type="text/javascript" src="ngbase.js"></script>
<style type="text/css">*{font-size: 14px; font-family: arial;}</style>
</head>
<body>
<div ng-controller="MainController">
Username: <input type="text" ng-model="username"/><br/><br/>
Password: <input type="text" ng-model="userpass"/><br/><br/>
<button ng-click="funSave()">Login</button>
</div>
</body>
</html>
</head>
<body>

</body>
</html>
------------------------------------------
ngbase.js

var app = angular.module('app',[]);
app.controller('MainController',function($scope,$http){
$scope.funSave = function(){
$http.post('ng-data.php',{'get_user':$scope.username,'get_pass':$scope.userpass}).success(function(data,status){
console.log(data.result);
})
}
})
------------------------------
ng-data.php

<?php 
$db = mysql_connect('localhost','root','');
mysql_select_db('db_school',$db);
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);

$user = $request->get_user;
$pass = $request->get_pass;
$sql = "INSERT into userlogin(username,userpass) values ('$user','$pass')";
$result=mysql_query($sql,$db);
$datArr = array();
$datArr['msg']=' Data has not saved';
if($result){
$datArr['msg'] = 'Data has been saved';
}
echo json_encode(array('result'=>$datArr));
?>
------------------
Demo

Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment