forked from richjava/jquery-bootstrap-ajax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
66 lines (65 loc) · 2.17 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bootstrap Dynamic Tabs with AJAX</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<style type="text/css">
.nav-container{
margin: 20px;
}
</style>
</head>
<body>
<div class="nav-container">
<ul class="nav nav-tabs">
<li class="active filled" data-id="0"><a data-toggle="tab" href="#i0">Section 1</a></li>
<li data-id="1"><a data-toggle="tab" href="#i1">Section 2</a></li>
<li data-id="2"><a data-toggle="tab" href="#i2">Section 3</a></li>
</ul>
<div class="tab-content">
<div id="i0" class="tab-pane fade in active">
<h3>Section 1</h3>
<p>Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.</p>
</div>
<div id="i1" class="tab-pane fade">
</div>
<div id="i2" class="tab-pane fade">
</div>
</div>
</div>
<script>
$(function() {
$(".nav").on("click", "li", function() {
var listItem = $(this);
//check if content has already been filled
if (!listItem.hasClass("filled")) {
fillData(listItem);
}
})
});
function fillData(listItem) {
//get index from list item "data-id" attribute
var id = listItem.data('id');
//send id through to tab-data.php through ajax request
var request = $.ajax({
url: "tab-data.php",
type: "POST",
dataType: "json",
data: {id: id}
});
request.done(function(data) {
$('#i'+id).html(data.content);
listItem.addClass("filled");
console.log("Filled section "+id);
});
request.fail(function(jqXHR, textStatus) {
console.log("Request failed: " + textStatus);
});
}
</script>
</body>
</html>