This isn’t really the place to get general WP coding help. The best place to start when trying to do AJAX in WP it here https://codex.wordpress.org/AJAX_in_Plugins. It says AJAX in plugins but the information there can be applied to doing AJAX anywhere in WP.