[Solved] Most efficient way to get custom database records from 20 buttons and 20 tables?


I suppose the easiest way would be to have the action or other parameters in the button itself, i.e.

<div class="mybutton" data-action="get_quote" data-type="literary">Get a literary quote</div>
<div class="mybutton" data-action="get_quote" data-type="political">Get a political quote</div>

data-foo="bar" attributes can easily be accessed in jQuery with .data("foo"), so you could change the jQuery around a bit to listen to clicks on the class instead of a single ID:

jQuery(document).ready(function($) {
    $(".mybutton").click( function() {
        jQuery.post("/wp-admin/admin-ajax.php", { "action": $(this).data("action"), "type": $(this).data("type")}, function(response) {
            var obj = JSON.parse(response);
            $("textarea[name="myDiv"]").val(obj.message);
            //console.log(response);
        });
    });
});

And voila, you’d now be able to pass data to the AJAX API by simply setting attributes in the button HTML. On the PHP-Side, you can check $_POST["type"] in your AJAX handler and react accordingly.

1

solved Most efficient way to get custom database records from 20 buttons and 20 tables?