Post request to PHP file in root folder

I have a form on my site to sign up subscribers to a Campaign Monitor list through their PHP API. I'd like to POST the data from this form to a PHP file placed in the 'cm-subscribe' folder placed in the root of my Statamic site.

I've used this code previously on non Statamic sites successfully, but I'm struggling to get it working in Statamic. Is what I'm trying to do possible? My PHP knowledge is pretty basic, so please forgive my ignorance if I'm missing anything obvious.

If anyone can point me in the right direction I would be hugely grateful! I've posted my JS snippet, and PHP file below to provide some context.

// newsletter submission

  const form = document.getElementById('submit-form');
  const formData = {
    name: form.querySelector('input#name'),
    email: form.querySelector('input#email'),
    submit: form.querySelector('button'),

  formData.submit.addEventListener('click', (e) => {
    const request = new XMLHttpRequest();
    const requestData = `name=${}&email=${}`'POST', '/cm-subscribe/campaign-monitor.php');
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

// php submission


require_once( __DIR__. '/createsend-php-master/csrest_subscribers.php');

function storeAddress($name,$email) {
  $wrap = new CS_REST_Subscribers($CM_LIST_ID, $CM_API_KEY);
  $result = $wrap->add(array(
    'EmailAddress' => $email,
    'Name' => $name,
    'ConsentToTrack' => 'yes',
    'Resubscribe' => true

function do_newsletter_response () {
  // Exit early if page render is not a POST request
  if ($_SERVER['REQUEST_METHOD'] != 'POST') return;



>>>>>>> Unanswered <<<<<<<
1 Reply