Bikin form Add

This commit is contained in:
Dita Aji Pratama 2025-06-17 10:46:25 +07:00
parent 65f4657d4d
commit 7c00802a77
2 changed files with 21 additions and 3 deletions

View File

@ -4,9 +4,9 @@
<link rel="stylesheet" href="//cdn.datatables.net/2.3.2/css/dataTables.dataTables.min.css"> <link rel="stylesheet" href="//cdn.datatables.net/2.3.2/css/dataTables.dataTables.min.css">
</head> </head>
<body> <body>
<input type="text" placeholder="Name" /> <input id="form-add-name" type="text" placeholder="Name" />
<input type="text" placeholder="Phone" /> <input id="form-add-phone" type="text" placeholder="Phone" />
<button>Create</button> <button id="button-add" type="button" onclick="buttonAdd()">Create</button>
<hr> <hr>
<table id="table-list"> <table id="table-list">
<thead> <thead>

View File

@ -40,3 +40,21 @@ var table = $('#table-list').DataTable({
}, },
], ],
}) })
function buttonAdd() {
var name = document.getElementById("form-add-name" ).value;
var phone = document.getElementById("form-add-phone" ).value;
var url = "http://localhost:11000/api/create";
var payload = {
"name" : name,
"phone" : phone
};
sendHttpRequest(url, "POST", payload, function (error, response) {
if (error) console.error("Error:", error);
else {
table.ajax.reload(null, false); // false means keep the current page
console.log("JSON Response:", response);
}
}, "application/json");
}