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) => {
    e.preventDefault();
    const request = new XMLHttpRequest();
    const requestData = `name=${formData.name.value}&email=${formData.email.value}`

    request.open('POST', '/cm-subscribe/campaign-monitor.php');
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.send(requestData);
  });

// php submission

<?php

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

function storeAddress($name,$email) {
  $CM_API_KEY = "$CM_API_KEY_GOES_HERE";
  $CM_LIST_ID = "$CM_LIST_ID_GOES_HERE";
  $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;

  storeAddress($_POST['name'],$_POST['email']);
}

do_newsletter_response();

?>