I'm counting votes on my website and when user submit the form, I want an ajax call that updates the total number of votes that is shown on the bottom of the page. I do this by counting number of rows in the table, like this:
$rowcount = $wpdb->get_var("SELECT COUNT(*) FROM _db7_forms");
This code works, now I wanna call it again when user submitted the form.
Hard to explain so I Show the code instead:
Where I show the number of votes:
<h1 class="intro"><?php echo $rowcount; ?>00000</h1>
The ajax call (I'm calling the function in another functions and I've tested it so it works):
function updateVotes(){
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: ({
action: "callDB"
}),
success: function (response){
console.log(response);
jQuery(".intro").html(response);
}
});
}
The query that check numbers of votes:
function callDB() {
$rowcount = $wpdb->get_var("SELECT COUNT(*) FROM _db7_forms");
}
This is what I got and I haven't really worked with AJAX before so i dont relly understand what I've written. The error I get now is myurl.com/wp-admin/admin-ajax.php 400 (Bad Request) The url is correct though and that file exists.
Just to make clear: The error given isn't my only problem, I'm positive that it won't give me the result I want. I need help with the next steps too.
urlvalue, perhaps)?updateVotes()orcallDBis throwing the error?add_action( 'wp_ajax_callDB', 'callDB' );(for logged-in users) and/oradd_action( 'wp_ajax_nopriv_callDB', 'callDB' );(for logged-out users) somewhere before or after thecallDBfunction? And incallDB(), you did have something likeecho $rowcount; exit;, right?