Posting to loop.php file

If that is the sum total of your loop.php, and you are accessing it directly which you are, your biggest problem is that you aren’t going to have access to any WordPress functions and yet you are trying to use WordPress functions. You are going to get fatal errors. Your script does nothing to boot WordPress. You need to do that.

You could include('wp-blog-header.php'); at the top of that script or you could do the right thing and use the AJAX API. It is a relatively simple conversion. Instead of a separate file create a callback function and hook it to the AJAX system.

function sample_ajax_callback() {
  // your script
}
add_action('wp_ajax_sample_ajax_callback_hookname','sample_ajax_callback');
add_action('wp_ajax_nopriv_sample_ajax_callback_hookname','sample_ajax_callback');

Your Javascript then POSTs to /wp-admin/admin-ajax.php with an action parameter named sample_ajax_callback_hookname.