-
Notifications
You must be signed in to change notification settings - Fork 0
/
snacks.html
62 lines (52 loc) · 2.35 KB
/
snacks.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SNACKS</title>
<link rel="stylesheet" type="text/css" href="./public/style.css">
<link
href="https://fonts.googleapis.com/css2?family=Alfa+Slab+One&family=Indie+Flower&family=Sacramento&display=swap"
rel="stylesheet">
<script type="text/javascript">
var arrayOfItem = []
function showSnacks() {
var data_file = "./public/snacks.json";
var http_request = new XMLHttpRequest();
http_request.onreadystatechange = function () {
if (http_request.readyState == 4) {
var obj = JSON.parse(http_request.responseText);
for (var i = 0; i < obj.length; i++) {
arrayOfItem[i] = obj[i]
var div = document.createElement('div');
div.className = 'box';
div.innerHTML = '<img class ="food_image" src="' + obj[i].image + '">' +
'<div class="inner_box">' +
'<h3 class="food_name">' + obj[i].name + '</h3>' +
'<p class="food_rating"> <br>Rating : ' + obj[i].rating + '</p>' +
'<span class="food_price"> Price: ' + obj[i].price + '</span><br><br>' +
'<button class="food_button" onClick=addToCart(' + i + ') >Add to cart</button>' + '</div>';
document.getElementById('snack').appendChild(div);
}
}
}
http_request.open("GET", data_file, true);
http_request.send();
}
function addToCart(idx) {
alert("Item added to cart")
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://willowy-selkie-72d923.netlify.app/addtocart", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(arrayOfItem[idx]));
console.log(arrayOfItem[idx]);
}
</script>
</head>
<body onload="showSnacks()">
<p class="menu_heading">Snacks</p>
<div class="box-container" id="snack" align="center">
</div>
</body>
</html>