Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prjcloud_65160270
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
65160270
prjcloud_65160270
Commits
d2a91a26
Commit
d2a91a26
authored
3 months ago
by
65160270
Browse files
Options
Downloads
Patches
Plain Diff
update-server
parent
02318cae
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
server.js
+21
-0
21 additions, 0 deletions
server.js
views/cart.ejs
+17
-11
17 additions, 11 deletions
views/cart.ejs
with
38 additions
and
11 deletions
server.js
+
21
−
0
View file @
d2a91a26
...
...
@@ -62,6 +62,27 @@ app.get('/order/checkout', isLoggedIn, (req, res) => {
res
.
render
(
'
checkout
'
);
});
app
.
post
(
"
/cart/update
"
,
async
(
req
,
res
)
=>
{
try
{
const
{
cartItemId
,
quantity
}
=
req
.
body
;
if
(
quantity
<
1
)
{
return
res
.
status
(
400
).
json
({
message
:
"
Quantity must be at least 1
"
});
}
// อัปเดตจำนวนสินค้าในฐานข้อมูล
await
pool
.
execute
(
"
UPDATE cart SET quantity = ? WHERE id = ?
"
,
[
quantity
,
cartItemId
]
);
res
.
json
({
success
:
true
,
message
:
"
Cart updated
"
});
}
catch
(
error
)
{
console
.
error
(
"
Update error:
"
,
error
);
res
.
status
(
500
).
json
({
message
:
"
Update failed
"
});
}
});
app
.
get
(
'
/register
'
,
(
req
,
res
)
=>
{
res
.
render
(
'
register
'
);
});
...
...
This diff is collapsed.
Click to expand it.
views/cart.ejs
+
17
−
11
View file @
d2a91a26
...
...
@@ -63,17 +63,23 @@
</div>
<script>
document.querySelectorAll(
'.edit
-btn
'
).forEach(button => {
button.addEventListener(
'
click
',
(event) => {
const cartItem = event.target.closest(
'
.cart-item
'
);
const
editButton
= cartItem.
querySelector('.edit-btn')
;
const
removeForm
= cartItem.querySelector(
'.remove-form')
;
document.querySelectorAll(
".update
-btn
"
).forEach(button => {
button.addEventListener(
"
click
", async
(event) => {
const cartItem = event.target.closest(
"
.cart-item
"
);
const
cartItemId
= cartItem.
dataset.id
;
const
quantity
= cartItem.querySelector(
"input[name='quantity']").value
;
// ซ่อนปุ่ม Edit
editButton.style.display = 'none';
const response = await fetch('/cart/update', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ cartItemId, quantity })
});
// แสดงปุ่ม Remove
removeForm.style.display = 'block';
if (response.ok) {
window.location.reload(); // รีเฟรชหน้าหลังอัปเดต
} else {
alert("Failed to update cart.");
}
});
});
</script>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment