<?php
$con= mysqli_connect('localhost', 'root','', 'table_append');
if (!$con) {
echo "connection_not_establishes";
die();
}
$sql="SELECT * from TA";
$result=mysqli_query($con,$sql);
if (mysqli_num_rows($result)>0) {
$row= mysqli_fetch_assoc($result);
print_r($row);
$arr = array('status' => 'success' ,'result' => $row );
echo json_encode($arr);
// die();
}
?>
<!DOCTYPE>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<button class="btn btn-success" type="button" id="insert">INSERT</button>
<div class="row">
<div class="col-sm-6">
<?php if(mysqli_num_rows($result) > 0): ?>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
<th>Update</th>
</tr>
</thead>
<tbody>
<?php while($row = mysqli_fetch_assoc($result)): ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['mobile']; ?></td>
<td><button type="button" class="btn btn-sm btn-success" id="<?=$row['user_id']?>" data-user="<?=$row['user_id']?>">Update</button></td>
<td><button type="button" class="btn btn-sm btn-success">Delete</button></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
</div>
<div class="modal fade" id="userDetialForm">
<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">Modal title</h4>
</div>
<div class="modal-body">
<form action="" method="POST" role="form" id="form">
<input type="hidden" name="user_id">
<div class="form-group">
<label for="">Name</label>
<input type="text" class="form-control" name="name" placeholder="Name">
</div>
<div class="form-group">
<label for="">Email</label>
<input type="text" class="form-control" name="email" placeholder="Email">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/popper.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('table td button').on('click', function(event) {
event.preventDefault();
/* Act on the event */
var user_id = $(this).attr('id');
// var user = $(this).data('table_append');
// ajax call
$.ajax({
url: 'ajax1.php',
type: 'POST',
dataType: 'JSON',
data: {user:user_id}
}).done(function(data){
console.log(data);
var result = data.result;
$('input[name="user_id"]').val(result.user_id);
$('input[name="name"]').val(result.name);
$('input[name="email"]').val(result.email);
$('#userDetialForm').modal('toggle');
});
});// end of click event
$('#userDetialForm').on('submit', function(event) {
event.preventDefault();
console.log($('#form').serialize());
$.ajax({
url:'ajax2.php',
type:'POST',
dataType:'JSON',
data:$('#form').serialize()
}).done(function(data){
console.log(data);
if(data.status == 'success'){
alert("OK");
window.location.reload();
}
});
// console.log( $(this).serialize() );
});
});
</script>
</body>
</html>
// here is ajax code to show data in table//
<?php
$con = mysqli_connect('localhost','root','','table_append');
if(!$con)
die('Unable to connect');
// $file = file_get_contents('sample.pdf');
// $sql = "INSERT INTO p_test (doc) VALUES('$file')";
// mysqli_query($con,$sql);
// echo "OK ".mysqli_error($con);
$userId = $_POST['user'];
$sql = "SELECT * FROM TA WHERE user_id = $userId";
$result = mysqli_query($con,$sql);
// echo "<pre>";
if( mysqli_num_rows($result) > 0 ){
$row = mysqli_fetch_assoc($result);
$arr = array('status' => 'success' ,'result' => $row );
echo json_encode($arr);
die();
// print_r($row);
// echo $file;
// echo '<object data="data:application/pdf;base64,'.$file.'" type="application/pdf" style="height:200px;width:60%"></object>';
// header('Location: dashboard.php');
// header("Content-Length: " . strlen($row['doc']) );
// header("Content-Type: application/octet-stream");
// header('Content-Disposition: attachment; filename="somefilename.pdf"');
// header("Content-Transfer-Encoding: binary\n");
// echo $row['doc'];
// die();
}else{
echo "NO Data Found";
}// end of else if
?>
// here is ajax code to update data
<?php
// print_r($_POST);
// die();
$con= mysqli_connect('localhost', 'root','', 'table_append');
if (!$con) {
echo "connection_not_establishes";
die();
}
if (isset($_POST['name'])&&isset($_POST['email'])) {
extract($_POST);
$sql = "UPDATE `TA` SET `name`='$name',`email`='$email' WHERE user_id='$user_id'";
// echo mysqli_error($con);
if ( mysqli_query($con,$sql)) {
// echo mysqli_error($con);
echo json_encode(['status'=>'success']);
}else{
echo mysqli_error($con);
}
}
?>
I want two buttons in a table, The table is showing data from database, one button to update data into row and one to delete, using ajax, I have created one button for update, which is targeted using id which is taken dynamically, that is by using id="". now the problem is that id cannot be repeated, so how could i target delete button to specifically target the same row.
Comments
Post a Comment