Passing form data via AJAX to REST API And displaying results

I got everything working but it may not be the most elegant of solutions.

I put the form text as well as the empty results div on a page. I created a small plugin that checks to see if we are on that page. If so, insert the JavaScript into the footer. Otherwise, do nothing.

This checks for the page we are on:

public function display_group_information($content)
{
    global $post;

    if ($post->post_name == 'groups-activities-and-events'){
        add_action('wp_footer', [$this, 'load_group_ajax']);
    }
    return $content;
}

This adds the required javascript:

public function load_group_ajax(){
?>
    <script>
        !function(r){function n(n).....("#grpSearch").submit(n)}(jQuery);
    </script>

<?php
}

Again, probably not the most elegant way of doing it but it works.