-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcart.php
More file actions
232 lines (194 loc) · 6.85 KB
/
cart.php
File metadata and controls
232 lines (194 loc) · 6.85 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
session_start();
// session_destroy();
if ($_SERVER['REQUEST_METHOD']=='POST')
{
if (isset($_POST['add_to_cart']))
{
if (isset($_SESSION['cart1'])) {
$myitems=array_column($_SESSION['cart1'], 'nameofproduct');
if (in_array($_POST['nameofproduct'], $myitems)) {
echo "<script>
alert('Item is already added');
window.location.href='index.php';
</script>";
// code...
}
else
{
$count=count($_SESSION['cart1']);
$_SESSION['cart1'][$count]=array('nameofproduct' =>$_POST['nameofproduct'],'price' =>$_POST['price'],'quantity' =>1 );
echo "<script>
alert('Item is added');
window.location.href='index.php';
</script>";
}
}
else{
$_SESSION['cart1'][0]=array('nameofproduct' =>$_POST['nameofproduct'],'price' =>$_POST['price'],'quantity' =>1 );
echo "<script>
alert('Item is added');
window.location.href='index.php';
</script>";
}
// code...
}
if (isset($_POST['remove_item']))
{
foreach ($_SESSION['cart1'] as $key => $value)
{
if($value['nameofproduct']==$_POST['nameofproduct']){
unset($_SESSION['cart1'][$key]);
$_SESSION['cart1']=array_values($_SESSION['cart1']);
echo "<script>
alert('Item removed');
window.location.href='index.php';
</script>";
}
}
// code...
}
}
?>
<!-- //indexpage -->
<!-- put this cod ein the cart page -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<title>Your Cart</title>
</head>
<style type="text/css">
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap');
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
html{
scroll-behavior:smooth;
}
body{
background-color: #756213;
background-image: linear-gradient(315deg, #756213 0%, #000000 74%);
background-position: center;
background-repeat: none;
background-size: 100%;
}
table {
width: 200%;
}
td, th {
text-align: left;
padding: 20px;
}
.container{
margin-top: 50px;
}
.table{
margin-top: 25px;
}
</style>
<body>
</div>
<div class="container">
<div class="row">
<div class="col-lg-12 " >
<h2 style="color:white;margin-left: 0px;margin-top: 0px;">Your Cart <i class="fa fa-shopping-cart" aria-hidden="true" ></i></h2>
</div>
<div class="col-lg-8">
<table class="table">
<thead>
<tr style="color:#ffffff">
<th scope="col">Item</th>
<th scope="col">Price</th>
<th scope="col">Quantity</th>
<th scope="col">Total</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<?php
$total=0;
if(isset($_SESSION['cart1'])){
foreach ($_SESSION['cart1'] as $key => $value) {
// print_r($value);
// code...
echo "
<tr>
<td style='color:white;font-size:15px;'>$value[nameofproduct]</td>
<td style='color:white;font-size:15px;'>$value[price]<input type='hidden' class='iprice' value='$value[price]'></td>
<td style='color:white;font-size:15px;outline:0px;'><input type='number'class='iquantity' onchange='subtotal()' value='$value[quantity]' min='1' max='10'></td>
<td class='itotal' style='color:white;font-size:17px;'></td>
<td>
<form action='cart.php' method='POST'>
<button class='btn btn-outline-primary btn-sm' name='remove_item'style='color:white;font-size:15px;'>remove</button>
<input type='hidden' name='nameofproduct' value='$value[nameofproduct]'>
</form>
</td>
</tr>
";
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
<div style="margin-top:auto;margin-left: 600px;" class="container">
<h3 style="color:white;font-size:24px">Subtotal: ₹ <span id="gtotal"></span></h3>
<a href="http://localhost/devstarters/webathon/index.php" class="btn btn-outline-danger btn-lg" style="font-size: 17px;margin-top: 12px;" target="_blank">Checkout</a>
</div>
<script type="text/javascript">
var gt=0;
var iprice=document.getElementsByClassName('iprice');
var iquantity=document.getElementsByClassName('iquantity');
var itotal=document.getElementsByClassName('itotal');
var gtotal=document.getElementById('gtotal')
function subtotal()
{
gt=0;
for (i =0;i<iprice.length;i++)
{
itotal[i].innerText=(iprice[i].value)*(iquantity[i].value);
gt=gt+(iprice[i].value)*(iquantity[i].value);
}
gtotal.innerText=gt;
}
subtotal();
</script>
<script>
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft= "0";
document.body.style.backgroundColor = "white";
}
var dropdown = document.getElementsByClassName("dropdown-btn");
var i;
for (i = 0; i < dropdown.length; i++) {
dropdown[i].addEventListener("click", function() {
this.classList.toggle("active");
var dropdownContent = this.nextElementSibling;
if (dropdownContent.style.display === "block") {
dropdownContent.style.display = "none";
} else {
dropdownContent.style.display = "block";
}
});
}
</script>
</body>
</html>