.htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ Index.php [L]
DB.php :
<?php
class DbConnect {
private $server = 'localhost';
private $dbname = 'hanuman';
private $user = 'root';
private $pass = '';
public function connect() {
try {
$conn = new PDO('mysql:host=' .$this->server .';dbname=' . $this->dbname, $this->user, $this->pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
} catch (\Exception $e) {
echo "Database Error: " . $e->getMessage();
}
}
}
?>
Index.php
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: *');
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Max-Age: 3600");
include("DB.php");
$dbConnect = new DbConnect();
$conn= $dbConnect->connect();
$method=$_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET' :
$sql="select * from emp";
$path=explode('/',$_SERVER['REQUEST_URI']);
if(isset($path[2]) && is_numeric($path[2])){
$sql .=" where eid =:eid order by eid desc";
$stmt = $conn->prepare($sql);
$stmt ->bindParam(':eid',$path[2]);
$stmt->execute();
$emp= $stmt->fetch(PDO::FETCH_ASSOC);
}else {
$stmt=$conn->prepare($sql);
$stmt->execute();
$emp= $stmt->fetchAll(PDO::FETCH_ASSOC);
}
echo json_encode($emp);
break;
case 'POST':
$emp= json_decode(file_get_contents('php://input'));
$sql="INSERT INTO `emp`(`NAME`, `ADDRESS`) VALUES (:name,:address)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':name',$emp->name);
$stmt->bindParam(':address',$emp->address);
if($stmt->execute()) {
$response= ['status' =>200, 'message' => 'Data Saved.'];
}
else {
$response= ['status' =>500, 'message' => 'Internal server error.'];
}
return json_encode($response);
break;
case 'PUT':
$emp= json_decode(file_get_contents('php://input'));
$sql="UPDATE emp SET NAME=:name,ADDRESS=:address WHERE EID=:eid";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':eid',$emp->eid);
$stmt->bindParam(':name',$emp->name);
$stmt->bindParam(':address',$emp->address);
if($stmt->execute()) {
$response= ['status' =>200, 'message' => 'Data Updated.'];
}
else {
$response= ['status' =>500, 'message' => 'Internal server error.'];
}
return json_encode($response);
break;
case 'DELETE':
$path=explode('/',$_SERVER['REQUEST_URI']);
$sql="delete from emp where eid=:eid";
$stmt = $conn->prepare($sql);
$stmt ->bindParam(':eid',$path[2]);
if($stmt->execute()) {
$response= ['status' =>200, 'message' => 'Data deleted.'];
}
else {
$response= ['status' =>500, 'message' => 'Internal server error.'];
}
break;
default:
echo 'Something went wrong.';
break;
}
?>
How call jquery ajax in php
Emp.php
<?php
if(isset($_POST['Name']))
{
$name=$_POST['Name'];
$addr=$_POST['Addr'];
$conn=mysqli_connect("localhost","root","","hanuman");
$sql="INSERT INTO `emp`( `NAME`, `ADDRESS`) VALUES ('$name','$addr')";
$result=mysqli_query($conn,$sql);
if($result)
{
echo "Data saved successfully.";
}
else
{
echo "Data can not saved.";
}
}
?>
Index.html
<!DOCTYPE html>
<html>
<header>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</header>
<body style="margin-top:10px ;">
<div class="container">
<form>
<div class="row">
<div class="col-md-4 text-right">
<label for="txtname">Name</label>
</div>
<div class="col-md-4">
<div class="form-group">
<input type="text" id="txtname" name="txtname" class="form-control">
</div>
</div>
<div class="col-md-4"></div>
</div>
<div class="row">
<div class="col-md-4 text-right">
<label for="txtadd">Address</label>
</div>
<div class="col-md-4">
<div class="form-group">
<textarea id="txtadd" name="txtadd" class="form-control"></textarea>
</div>
</div>
<div class="col-md-4"></div>
</div>
<div class="row">
<div class="col-md-4">
</div>
<div class="col-md-4">
<div class="form-group">
<input type="button" id="btnsave" value="Submit" class="btn btn-primary">
<input type="reset" id="btnsave" value="Reset" class="btn btn-primary">
</div>
</div>
<div class="col-md-4">
</div>
</div>
</form>
</div>
</body>
</html>
<script type="text/javascript">
$(function(){
$('#btnsave').click(function(){
var name=$('#txtname').val();
var addr=$('#txtadd').val();
$.post("Emp.php",{
"Name":name,
"Addr":addr
},function(data,status){
alert(data);
});
})
});
</script>
Call Api In Php :
<!DOCTYPE html>
<html>
<header>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</header>
<body style="margin-top:10px ;">
<div class="container">
<form>
<div class="row">
<div class="col-md-4 text-right">
<label for="txtname">Name</label>
</div>
<div class="col-md-4">
<div class="form-group">
<input type="text" id="txtname" name="txtname" class="form-control">
</div>
</div>
<div class="col-md-4"></div>
</div>
<div class="row">
<div class="col-md-4 text-right">
<label for="txtadd">Address</label>
</div>
<div class="col-md-4">
<div class="form-group">
<textarea id="txtadd" name="txtadd" class="form-control"></textarea>
</div>
</div>
<div class="col-md-4"></div>
</div>
<div class="row">
<div class="col-md-4">
</div>
<div class="col-md-4">
<div class="form-group">
<input type="button" id="btnsave" value="Submit" class="btn btn-primary">
<input type="reset" id="btnsave" value="Reset" class="btn btn-primary">
</div>
</div>
<div class="col-md-4">
</div>
</div>
</form>
</div>
</body>
</html>
<script type="text/javascript">
$(function(){
$('#btnsave').click(function(){
var name=$('#txtname').val();
var addr=$('#txtadd').val();
$.post("http://localhost:8080/rabiapi/Index/Save",JSON.stringify({
"name":name,
"address":addr
}),function(data,status){
alert(data);
});
})
});
</script>
No comments:
Post a Comment