· 9 years ago · Apr 13, 2017, 01:42 PM
1program index.php
2<!doctype html>
3<html lang="en">
4<head>
5<title>Menghapus Data Tanpa Merefresh Halaman dengan Ajax</title>
6<meta content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" name="viewport"/>
7<meta content="Aguzrybudy" name="author"/>
8<link href="css/bootstrap.css" rel="stylesheet">
9<script src="js/jquery.min.js"></script>
10<script src="js/bootstrap.min.js"></script>
11</head>
12<body>
13
14<div class="container">
15<h2>
16Menghapus Data Tanpa Merefresh Halaman dengan Ajax</h2>
17
18<p>
19<a href="#" class="btn btn-success" data-target="#ModelAdd" data-toggle="model">Add Data</a></p>
20<table id="mytable" class="table table-bordered"><thead> <th>id</th> <th>Name</th> <th>Alamat</th> <th>Jabatan</th> </thead> <?php
21 include "koneksi.php";
22 $no = 0;
23 $model=mysqli_query($kon,"SELECT * FROM model");
24 while($r=mysqli_fetch_array($model)){
25 $no++;
26
27?>
28<tr> <td><?php echo $no; ?></td> <td><?php echo $r['model_nama']; ?></td> <td><?php echo $r['description']; ?></td> <td>
29<a href="#" class='open_model' id='<?php echo $r['model_id']; ?>'>Edit</a>
30<a href="#" onclick="confirm_modal('proses_delete.php?&model_id=<?php echo $r['model_id']; ?>');">Delete</a>
31</td> </tr>
32<?php } ?> </table>
33</div>
34<script type="text/javascript">
35 $(document).ready(function () {
36 $(".open_modal").click(function(e) {
37 var m = $(this).attr("id");
38 $.ajax({
39 url: "modal_edit.php",
40 type: "GET",
41 data : {model_id: m,},
42 success: function (ajaxData){
43 $("#ModalEdit").html(ajaxData);
44 $("#ModalEdit").modal('show',{backdrop: 'true'});
45 }
46 });
47 });
48 });
49</script>
50
51<script type="text/javascript">
52 function confirm_modal(delete_url)
53 {
54 $('#modal_delete').modal('show', {backdrop: 'static'});
55 document.getElementById('delete_link').setAttribute('href' , delete_url);
56 }
57</script>
58
59</body>
60</html>
61
62koneksi.php
63<?php
64error_reporting(E_ALL ^ E_DEPRECATED);
65$host = "localhost";
66$user = "root";
67$pass = "";
68$dbName = "teknologiweb2";
69
70$kon = mysqli_connect($host, $user, $pass);
71if (!$kon)
72 die("Gagal Koneksi...");
73
74$hasil = mysqli_select_db($kon, $dbName);
75if (!$hasil) {
76 $hasil = mysqli_query($kon, "CREATE DATABASE $dbName");
77 if (!$hasil)
78 die("Gagal Buat Database");
79 else
80 $hasil = mysqli_select_db($kon, $dbName);
81 if (!$hasil) die ("Gagal Konek Database");
82}
83
84$sqlTabelModel = "create table if not exists model (
85 model_id int(11) auto_increment not null primary key,
86 model_nama varchar(10) not null,
87 description text,
88 date timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
89 KEY(model_id))" ;
90mysqli_query ($kon, $sqlTabelModel) or die("Gagal Buat Tabel Model");
91
92
93?>
94
95delete.php
96<?php
97include "koneksi.php";
98$model_id=$_GET['model_id'];
99$model=mysqli_query($kon,"Delete FROM model WHERE model_id='$model_id'");
100header('location:index.php');
101?>