Json read
json_read.php
<!DOCTYPE html>
<html>
<head>
<title>read json</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="table_responsive">
<h1>Boot Table</h1>
<div class="well clearfix">
<div class="pull-right"><button type="button" class="btn btn-xs btn-primary" id="command-add" data-row-id="0">
<span class="glyphicon glyphicon-plus"></span> Record</button></div></div>
<table class="table table-bordered table-striped" id="employee_table" data-toggle="bootgrid">
<tr>
<th>Name</th>
<th>Address</th>
<th>Gender</th>
<th>Designation</th>
<th>Age</th>
<th>Action</th>
</tr>
</table>
</div>
</div>
<div id="add_model" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Add Employee</h4>
</div>
<div class="modal-body">
<form method="post" id="frm_add" name="frm_add">
<input type="hidden" value="add" name="action" id="action">
<div class="form-group">
<label for="id" class="control-label">ID:</label>
<input type="text" class="form-control" id="id" name="id"/>
</div>
<div class="form-group">
<label for="name" class="control-label">Name:</label>
<input type="text" class="form-control" id="name" name="name"/>
</div>
<div class="form-group">
<label for="address" class="control-label">Address:</label>
<input type="text" class="form-control" id="address" name="address"/>
</div>
<div class="form-group">
<label for="gender" class="control-label">Gender:</label>
<input type="text" class="form-control" id="gender" name="gender"/>
</div>
<div class="form-group">
<label for="designation" class="control-label">Designation:</label>
<input type="text" class="form-control" id="designation" name="designation"/>
</div>
<div class="form-group">
<label for="age" class="control-label">Age:</label>
<input type="text" class="form-control" id="age" name="age"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="btn_add" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
<div id="edit_model" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Edit Employee</h4>
</div>
<div class="modal-body">
<form method="post" id="frm_edit">
<input type="hidden" value="edit" name="action" id="action">
<input type="hidden" value="0" name="edit_id" id="edit_id">
<div class="form-group">
<label for="name" class="control-label">Name:</label>
<input type="text" class="form-control" id="edit_name" name="edit_name"/>
</div>
<div class="form-group">
<label for="salary" class="control-label">Salary:</label>
<input type="text" class="form-control" id="edit_salary" name="edit_salary"/>
</div>
<div class="form-group">
<label for="salary" class="control-label">Age:</label>
<input type="text" class="form-control" id="edit_age" name="edit_age"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="btn_edit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
<script type="text/javascript">
$(document).ready(function(){
$.getJSON("r_json.json", function(data){
var employee_data=' ';
$.each(data, function(key, value){
employee_data +='<tr>';
employee_data +='<td>'+ value.name +'</td>';
employee_data +='<td>'+ value.address +'</td>';
employee_data +='<td>'+ value.gender +'</td>';
employee_data +='<td>'+ value.designation +'</td>';
employee_data +='<td>'+ value.age +'</td>';
employee_data +='<td> <button type="button" onClick="mycfunction('+ value.id +')" value="'+ value.id +'" id="check" class="btn btn-danger">Del</button> <button type="button" onClick="myefunction('+ value.id +')" value="'+ value.id +'" id="edit_form" class="btn btn-danger">Edit</button> </td>'
employee_data +='</tr>';
});
$('#employee_table').append(employee_data);
});
});
function mycfunction(sid){
alert(sid);
$.ajax({
url: "delete.php",
type: "POST",
data: {
id: sid,
},
success: function (result) {
location.reload(true);
}
});
}
$( "#command-add" ).click(function() {
$('#add_model').modal('show');
});
$( "#btn_add" ).click(function() {
ajaxAction('add');
});
function ajaxAction(action) {
$('#'+action+'_model').modal('hide');
location.reload(true);
data = $("#frm_"+action).serialize();
//alert(data);
$.ajax({
type: "POST",
url: "response.php",
data: data,
dataType: "json",
success: function(response)
{
}
});
}
</script>
delete.php
<?php
$id = $_POST['id'];
if (!preg_match('/^\d+$/', $id)) {
die('Don’t hack us.');
}
$file = 'r_json.json';
$data = file_get_contents($file);
$list = json_decode($data);
for ($i = 0; $i < count($list); $i++) {
if ($list[$i]->id === $id) {
unset($list[$i]);
break;
}
}
$list = array_values($list);
$fp = fopen($file, 'w');
fwrite($fp, json_encode($list));
fclose($fp);
response.php
<?php
if ($_POST['add'] !=' ') {
$id = $_POST['id'];
$name = $_POST['name'];
$address = $_POST['address'];
$gender = $_POST['gender'];
$designation = $_POST['designation'];
$age = $_POST['age'];
$additionalArray = array(
'id' => $id,
'name' => $name,
'address' => $address,
'gender' => $gender,
'designation' => $designation,
'age' => $age
);
$inp = file_get_contents('r_json.json');
$tempArray = json_decode($inp);
array_push($tempArray, $additionalArray);
$jsonData = json_encode($tempArray);
file_put_contents('r_json.json', $jsonData);
echo "insert";
//return true;
}else
{
return false;
}
<!DOCTYPE html>
<html>
<head>
<title>read json</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="table_responsive">
<h1>Boot Table</h1>
<div class="well clearfix">
<div class="pull-right"><button type="button" class="btn btn-xs btn-primary" id="command-add" data-row-id="0">
<span class="glyphicon glyphicon-plus"></span> Record</button></div></div>
<table class="table table-bordered table-striped" id="employee_table" data-toggle="bootgrid">
<tr>
<th>Name</th>
<th>Address</th>
<th>Gender</th>
<th>Designation</th>
<th>Age</th>
<th>Action</th>
</tr>
</table>
</div>
</div>
<div id="add_model" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Add Employee</h4>
</div>
<div class="modal-body">
<form method="post" id="frm_add" name="frm_add">
<input type="hidden" value="add" name="action" id="action">
<div class="form-group">
<label for="id" class="control-label">ID:</label>
<input type="text" class="form-control" id="id" name="id"/>
</div>
<div class="form-group">
<label for="name" class="control-label">Name:</label>
<input type="text" class="form-control" id="name" name="name"/>
</div>
<div class="form-group">
<label for="address" class="control-label">Address:</label>
<input type="text" class="form-control" id="address" name="address"/>
</div>
<div class="form-group">
<label for="gender" class="control-label">Gender:</label>
<input type="text" class="form-control" id="gender" name="gender"/>
</div>
<div class="form-group">
<label for="designation" class="control-label">Designation:</label>
<input type="text" class="form-control" id="designation" name="designation"/>
</div>
<div class="form-group">
<label for="age" class="control-label">Age:</label>
<input type="text" class="form-control" id="age" name="age"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="btn_add" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
<div id="edit_model" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Edit Employee</h4>
</div>
<div class="modal-body">
<form method="post" id="frm_edit">
<input type="hidden" value="edit" name="action" id="action">
<input type="hidden" value="0" name="edit_id" id="edit_id">
<div class="form-group">
<label for="name" class="control-label">Name:</label>
<input type="text" class="form-control" id="edit_name" name="edit_name"/>
</div>
<div class="form-group">
<label for="salary" class="control-label">Salary:</label>
<input type="text" class="form-control" id="edit_salary" name="edit_salary"/>
</div>
<div class="form-group">
<label for="salary" class="control-label">Age:</label>
<input type="text" class="form-control" id="edit_age" name="edit_age"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="btn_edit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
<script type="text/javascript">
$(document).ready(function(){
$.getJSON("r_json.json", function(data){
var employee_data=' ';
$.each(data, function(key, value){
employee_data +='<tr>';
employee_data +='<td>'+ value.name +'</td>';
employee_data +='<td>'+ value.address +'</td>';
employee_data +='<td>'+ value.gender +'</td>';
employee_data +='<td>'+ value.designation +'</td>';
employee_data +='<td>'+ value.age +'</td>';
employee_data +='<td> <button type="button" onClick="mycfunction('+ value.id +')" value="'+ value.id +'" id="check" class="btn btn-danger">Del</button> <button type="button" onClick="myefunction('+ value.id +')" value="'+ value.id +'" id="edit_form" class="btn btn-danger">Edit</button> </td>'
employee_data +='</tr>';
});
$('#employee_table').append(employee_data);
});
});
function mycfunction(sid){
alert(sid);
$.ajax({
url: "delete.php",
type: "POST",
data: {
id: sid,
},
success: function (result) {
location.reload(true);
}
});
}
$( "#command-add" ).click(function() {
$('#add_model').modal('show');
});
$( "#btn_add" ).click(function() {
ajaxAction('add');
});
function ajaxAction(action) {
$('#'+action+'_model').modal('hide');
location.reload(true);
data = $("#frm_"+action).serialize();
//alert(data);
$.ajax({
type: "POST",
url: "response.php",
data: data,
dataType: "json",
success: function(response)
{
}
});
}
</script>
delete.php
<?php
$id = $_POST['id'];
if (!preg_match('/^\d+$/', $id)) {
die('Don’t hack us.');
}
$file = 'r_json.json';
$data = file_get_contents($file);
$list = json_decode($data);
for ($i = 0; $i < count($list); $i++) {
if ($list[$i]->id === $id) {
unset($list[$i]);
break;
}
}
$list = array_values($list);
$fp = fopen($file, 'w');
fwrite($fp, json_encode($list));
fclose($fp);
response.php
<?php
if ($_POST['add'] !=' ') {
$id = $_POST['id'];
$name = $_POST['name'];
$address = $_POST['address'];
$gender = $_POST['gender'];
$designation = $_POST['designation'];
$age = $_POST['age'];
$additionalArray = array(
'id' => $id,
'name' => $name,
'address' => $address,
'gender' => $gender,
'designation' => $designation,
'age' => $age
);
$inp = file_get_contents('r_json.json');
$tempArray = json_decode($inp);
array_push($tempArray, $additionalArray);
$jsonData = json_encode($tempArray);
file_put_contents('r_json.json', $jsonData);
echo "insert";
//return true;
}else
{
return false;
}
Comments
Post a Comment