In wp-admin I have a table that extracts data from the database (data comes from a form filled in by users) . The request to the database to delete some elements is contained in form-plugin.php (plugins directory), function 'delete_custom_form_data()'.
the table structure and the database ID delete button is contained in functions.php
The delete.js file in /js takes the ID from functions.php and passes it to form-plugin.php, which respectively deletes the element from the database.
Whatever I do, delete.js still gives error 400 on request to form-plugin
functions.php:
function display_custom_form_data() {
global $wpdb;
$table_name = $wpdb->prefix . 'custom_form_data';
$data = $wpdb->get_results("SELECT * FROM $table_name", ARRAY_A);
?>
<div class="wrap">
<h2>Jurnalul programărilor</h2>
<input type="hidden" id="ajax-url" value="<?php echo admin_url('admin-ajax.php'); ?>">
<table class="wp-list-table widefat striped">
<thead>
<tr>
<th>ID</th>
<th>Nume</th>
<th>Email</th>
<th>Telefon</th>
<th>Specializare</th>
<th>Data Programării</th>
<th>Ora Programării</th>
<th>Data Aplicării</th>
<th>Acțiune</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['spec']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['time']; ?></td>
<td><?php echo $row['sub_time']; ?></td>
<td><button class="delete-button" data-id="<?php echo $row['id']; ?>">Șterge</button></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
}
add_action('admin_menu', 'custom_form_admin_page');
add_action('admin_menu', 'custom_form_admin_page');
delete.js:
document.addEventListener('DOMContentLoaded', function() {
const ajaxUrlElement = document.getElementById('ajax-url');
if (ajaxUrlElement) {
const ajaxUrl = ajaxUrlElement.value;
const deleteButtons = document.querySelectorAll('.delete-button');
deleteButtons.forEach(function(button) {
button.addEventListener('click', function() {
const id = encodeURIComponent(this.getAttribute('data-id'));
if (confirm('Ești sigur că vrei să ștergi acest rând?')) {
fetch(ajaxUrl, {
method: 'POST',
headers: {
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
body: 'action=delete_custom_form_data&id=' +id
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok. Status: ' + response.status + '. Body: ' + response.statusText);
}
location.reload();
})
.catch(error => {
console.error('There has been a problem with your fetch operation: ', error);
});
}
});
});
}
});
plugin/form-plugin.php:
function delete_custom_form_data() {
if (isset($_POST['id'])) {
$id = $_POST['id'];
error_log('ID-ul primit pentru ștergere: ' . $id);
global $wpdb;
$table_name = $wpdb->prefix . 'custom_form_data';
$result = $wpdb->delete($table_name, array('id' => $id));
if ($result === false) {
echo 'Eroare la ștergerea rândului cu ID-ul ' . $id;
} else {
echo 'Rândul cu ID-ul ' . $id . ' a fost șters cu succes!';
}
} else {
echo 'ID-ul nu a fost furnizat.';
}
}
add_action('admin_post_custom_delete_action', 'delete_custom_form_data');
add_action('admin_post_nopriv_custom_delete_action', 'delete_custom_form_data');
Am căutat mai multe soluții, dar nu am reușit să găsesc cauza problemei
wp_ajax_*
and `wp_ajax_nopriv_*.