0

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.

8
  • Do you call exactly the same Ajax function in another function successfully, or did you change it at all for this usage (changing the url value, perhaps)? Commented Feb 22, 2018 at 20:23
  • Do you know for certain if updateVotes() or callDB is throwing the error? Commented Feb 22, 2018 at 20:25
  • @223seneca I'm only calling this function when the form is successully submitted Commented Feb 22, 2018 at 20:28
  • How do I test that? Commented Feb 22, 2018 at 20:30
  • 1
    Did you actually add add_action( 'wp_ajax_callDB', 'callDB' ); (for logged-in users) and/or add_action( 'wp_ajax_nopriv_callDB', 'callDB' ); (for logged-out users) somewhere before or after the callDB function? And in callDB(), you did have something like echo $rowcount; exit;, right? Commented Feb 23, 2018 at 4:46

1 Answer 1

2

Did you try like this?

function callDB() {
    global $wpdb;
    $rowcount = $wpdb->get_var("SELECT COUNT(*) FROM wp_posts");
    echo $rowcount;
    exit;
}
add_action( 'wp_ajax_callDB', 'callDB' ); 
add_action( 'wp_ajax_nopriv_callDB', 'callDB' );


    function updateVotes(){
        $.ajax({
            url: '<?php echo admin_url('admin-ajax.php'); ?>',
            type: 'post',
            data: ({
                action: "callDB"
            }),
            success: function (response){
                console.log(response);
                jQuery(".intro").text(response);
            }
        });
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Okay so now it makes the ajax call but doesnt send back the right thing to the <h1>, which makes sense because I just echo it? How do I send a value with response?
OKay it worked!! I saw now that you used another db table! Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.