У меня есть две кнопки в форме, и я хочу получить данные моего метода create post, чтобы создать список данных в строке. Проблема в том, что когда я перезагружаю страницу, список исчезает, и страница перезагружается, когда я нажимаю одну из кнопок.
document.addEventListener('DOMContentLoaded', function() {
// Load the list of posts from local storage
const posts = JSON.parse(localStorage.getItem('posts')) || [];
// Check each post
posts.forEach((post, index) => {
// AJAX call to check if the post exists
fetch('', {
method: 'POST',
body: JSON.stringify({
action: 'check_post',
id: post.id
}),
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => {
if (data.exists === false) {
// If the post doesn't exist, remove it from the DOM
const pageElement = document.getElementById(post.id);
if (pageElement) {
pageElement.parentNode.removeChild(pageElement);
}
// Remove the post from the list
posts.splice(index, 1);
// Store the updated list of posts in local storage
localStorage.setItem('posts', JSON.stringify(posts));
}
});
});
// Add event listeners to buttons
document.querySelectorAll('button#gutenberg_button, button#elementor_button').forEach(button => {
button.addEventListener('click', function(e) {
e.preventDefault();
const form = this.closest('form'); // Find the closest form to the clicked button
const formData = new FormData(form);
formData.append('action', 'create_post');
fetch('', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
// Create table row
const tableRow = document.createElement('tr');
tableRow.id = 'post-' + data.id; // Set the id attribute to the post ID
// Create cells for title, date, and actions
const titleCell = document.createElement('td');
titleCell.textContent = data.title;
const dateCell = document.createElement('td');
dateCell.textContent = data.date;
const actionsCell = document.createElement('td');
actionsCell.classList.add('actions'); // Add a class for styling
// Create buttons for actions
const editButton = document.createElement('button');
editButton.classList.add('editBtn');
editButton.innerHTML = '[i][/i]';
editButton.addEventListener('click', () => {
// Add functionality for editing the post
});
const deleteButton = document.createElement('button');
deleteButton.classList.add('deleteBtn');
deleteButton.innerHTML = '[i][/i]';
deleteButton.addEventListener('click', () => {
deletePost(data.id); // Call the deletePost function with the post ID
});
const viewButton = document.createElement('button');
viewButton.classList.add('viewBtn');
viewButton.innerHTML = '[i][/i]';
viewButton.addEventListener('click', () => {
// Add functionality for viewing the post
});
// Append buttons to actions cell
actionsCell.appendChild(editButton);
actionsCell.appendChild(deleteButton);
actionsCell.appendChild(viewButton);
// Append cells to the table row
tableRow.appendChild(titleCell);
tableRow.appendChild(dateCell);
tableRow.appendChild(actionsCell);
// Get the table body and append the updated row
const tableBody = document.getElementById('generated-pages');
tableBody.appendChild(tableRow);
// Add the new post to the list of posts
posts.push(data);
localStorage.setItem('posts', JSON.stringify(posts));
showNotification();
});
});
});
});
function showNotification() {
const notification = document.getElementById('notification');
notification.style.display = 'block';
// Masquer la notification après 5 secondes
setTimeout(function() {
notification.style.display = 'none';
}, 5000);
}
function deletePost(postId) {
jQuery.ajax({
url: "admin-ajax.php",
type: "POST",
data: {
action: "delete_post",
id: postId
},
success: function(response) {
if (response === "success") {
// Remove the pageElement
jQuery("#"+postId).remove();
}
}
});
}
function deletePostDetails(postId) {
// Remove the title
jQuery("#title-"+postId).remove();
// Remove the date
jQuery("#date-"+postId).remove();
}
Подробнее здесь: [url]https://stackoverflow.com/questions/78487951/how-to-retain-data-after-reload-the-page[/url]
У меня есть две кнопки в форме, и я хочу получить данные моего метода create post, чтобы создать список данных в строке. Проблема в том, что когда я перезагружаю страницу, список исчезает, и страница перезагружается, когда я нажимаю одну из кнопок. [code] document.addEventListener('DOMContentLoaded', function() { // Load the list of posts from local storage const posts = JSON.parse(localStorage.getItem('posts')) || [];
// Check each post posts.forEach((post, index) => { // AJAX call to check if the post exists fetch('', { method: 'POST', body: JSON.stringify({ action: 'check_post', id: post.id }), headers: { 'Content-Type': 'application/json' } }) .then(response => response.json()) .then(data => { if (data.exists === false) { // If the post doesn't exist, remove it from the DOM const pageElement = document.getElementById(post.id); if (pageElement) { pageElement.parentNode.removeChild(pageElement); }
// Remove the post from the list posts.splice(index, 1);
// Store the updated list of posts in local storage localStorage.setItem('posts', JSON.stringify(posts)); } }); });
// Add event listeners to buttons document.querySelectorAll('button#gutenberg_button, button#elementor_button').forEach(button => { button.addEventListener('click', function(e) { e.preventDefault(); const form = this.closest('form'); // Find the closest form to the clicked button const formData = new FormData(form); formData.append('action', 'create_post');
fetch('', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { // Create table row const tableRow = document.createElement('tr'); tableRow.id = 'post-' + data.id; // Set the id attribute to the post ID
// Create cells for title, date, and actions const titleCell = document.createElement('td'); titleCell.textContent = data.title;
const actionsCell = document.createElement('td'); actionsCell.classList.add('actions'); // Add a class for styling
// Create buttons for actions const editButton = document.createElement('button'); editButton.classList.add('editBtn'); editButton.innerHTML = '[i][/i]'; editButton.addEventListener('click', () => { // Add functionality for editing the post });
const deleteButton = document.createElement('button'); deleteButton.classList.add('deleteBtn'); deleteButton.innerHTML = '[i][/i]'; deleteButton.addEventListener('click', () => { deletePost(data.id); // Call the deletePost function with the post ID });
const viewButton = document.createElement('button'); viewButton.classList.add('viewBtn'); viewButton.innerHTML = '[i][/i]'; viewButton.addEventListener('click', () => { // Add functionality for viewing the post });
// Append buttons to actions cell actionsCell.appendChild(editButton); actionsCell.appendChild(deleteButton); actionsCell.appendChild(viewButton);
// Append cells to the table row tableRow.appendChild(titleCell); tableRow.appendChild(dateCell); tableRow.appendChild(actionsCell);
// Get the table body and append the updated row const tableBody = document.getElementById('generated-pages'); tableBody.appendChild(tableRow);
// Add the new post to the list of posts posts.push(data); localStorage.setItem('posts', JSON.stringify(posts)); showNotification(); }); }); }); });
function showNotification() { const notification = document.getElementById('notification'); notification.style.display = 'block';
// Masquer la notification après 5 secondes setTimeout(function() { notification.style.display = 'none'; }, 5000); } function deletePost(postId) { jQuery.ajax({ url: "admin-ajax.php", type: "POST", data: { action: "delete_post", id: postId }, success: function(response) { if (response === "success") { // Remove the pageElement jQuery("#"+postId).remove(); } } }); }
function deletePostDetails(postId) { // Remove the title jQuery("#title-"+postId).remove();
// Remove the date jQuery("#date-"+postId).remove(); }