์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- Absolute
- AGI
- ai
- AI agents
- AI engineer
- AI researcher
- ajax
- algorithm
- Algorithms
- aliases
- Array ๊ฐ์ฒด
- ASI
- bayes' theorem
- Bit
- Blur
- BOM
- bootstrap
- canva
- challenges
- ChatGPT
- Today
- In Total
A Joyful AI Research Journey๐ณ๐
[10] 230613 ํ ํ๋ก์ ํธ (3) 10์ผ ์ฐจ) Ajax ์ฝ๋ ๋ฐ ๋งํฌ ๊ธ๋ค ๋ณธ๋ฌธ
[10] 230613 ํ ํ๋ก์ ํธ (3) 10์ผ ์ฐจ) Ajax ์ฝ๋ ๋ฐ ๋งํฌ ๊ธ๋ค
yjyuwisely 2023. 6. 13. 16:34The Java code you have implemented handles the server-side logic, such as updating the 'visible' column in the MySQL database when the button is clicked.
The JavaScript code, on the other hand, handles the client-side behavior, which is updating the button text dynamically
without reloading the page.
When the button is clicked, the JavaScript code will execute in the browser, and you can make an asynchronous request to the server-side Java code using AJAX or fetch API to update the database.
Once the response is received, you can then update the button text in the browser using JavaScript.
<button id="toggleButton" data-itemname="actualitemname" data-visibility="Y">Stop using</button>
<script>
var toggleButton = document.getElementById("toggleButton");
toggleButton.addEventListener("click", function() {
var itemName = toggleButton.getAttribute("data-itemname");
var visibility = toggleButton.getAttribute("data-visibility");
// Make an AJAX request to update the column value in MySQL
var xhr = new XMLHttpRequest();
xhr.open("POST", "/itemDetails/" + itemName + "/toggleVisibility", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// Update the button's text and visibility attribute based on the response
if (visibility === "Y") {
toggleButton.textContent = "Start using";
toggleButton.setAttribute("data-visibility", "N");
} else {
toggleButton.textContent = "Stop using";
toggleButton.setAttribute("data-visibility", "Y");
}
}
};
xhr.send();
});
</script>
<script>
var button = document.getElementById('toggleButton');
button.addEventListener('click', function() {
if (button.innerText === '์ฌ์ฉ ์ค์ง') {
button.innerText = '์ฌ์ฌ์ฉ';
} else {
button.innerText = '์ฌ์ฉ ์ค์ง';
}
// Call the toggleVisibility function passing the name as a parameter
toggleVisibility('${itemDetails.name}');
});
function toggleVisibility(name) {
// Perform the necessary actions to update the visibility status in the database
// You can use AJAX or make a form submission to your controller endpoint to handle the update
// Example AJAX code
// You need to adjust this code according to your backend setup
$.ajax({
url: '/itemDetails/' + name + '/toggleVisibility',
type: 'POST',
success: function(response) {
// Handle the success response if needed
console.log(response);
},
error: function(xhr, status, error) {
// Handle the error response if needed
console.log(xhr.responseText);
}
});
}
</script>
function toggleVisibility(name) {
// Make an AJAX request to update the data in the database
// You can use libraries like jQuery or fetch API to make the AJAX request
// Example using jQuery AJAX
$.ajax({
url: '/itemDetails/' + name + '/toggleVisibility',
type: 'POST',
success: function(response) {
// Handle the success response
// For example, you can update the button text or perform any other necessary actions
console.log('Data updated successfully');
},
error: function(xhr, status, error) {
// Handle the error response
console.log('Error updating data:', error);
}
});
}
https://terianp.tistory.com/95
https://azderica.github.io/00-javascript-ajax/
https://hei-jayden.tistory.com/248