Kirby 3 POST request to external API not working

I am having an issue with making a POST request to an external API.

I recently migrated from Kirby 2 → 3. The code below to make a POST request was working at some point, presumably before migrating.

When I submit I get a 404 with this error on my local:

POST http://localhost:8888/plainkit-main/index.php 404 (Not Found)

I am wondering, is this a Kirby 3 routing issue?

Php is not my first language and I am still a Kirby novice, so I am wondering if there is a better way entirely to go about this.

Here is my code:

<?php $key = page("contact")->artlogic()->html(); ?>
<div class="signup">
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="artlogic-signup">
            <input type="text" id="email" name="email" placeholder="Email Address">
            <button class="subscribe" type="submit" name="submit" value="Subscribe">Subscribe</button>
    </form>
    <div id="response"></div>
</div>
<?php
if(isset($_POST['email'])) {
    $apiKey = $key;
    $email = $_POST['email'];

    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => "<URL HERE>",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "api_key=$apiKey&email=$email",
      CURLOPT_HTTPHEADER => array(
        "Content-Type: application/x-www-form-urlencoded"
      ),
    ));

    $response = curl_exec($curl);
    curl_close($curl);
}
?>

<script>
   $(document).ready(function() {
    $("#artlogic-signup").submit(function(e) {
        e.preventDefault();
        var email = $("#email").val();
        console.log(email)
        var emailRegex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (emailRegex.test(email)) {
            $.ajax({
                type: 'POST',
                url: '<?php echo $_SERVER['PHP_SELF']; ?>',
                data: { email: email },
                success: function(response) {
                    $("#response").html("<p class='success'>Thank you! Your email has been added to the mailing list.</p>");
                    $('#artlogic-signup')[0].reset();
                }
            });
        } else {
            $("#response").html("<p class='error'>Please provide a valid email address</p>");
        }
    });
});
</script>

What does this give you? You’d better use $page->url() here.

@texnixe, Thank you for the quick response yesterday! That was the the solution, $_SERVER['PHP_SELF'] was hitting the wrong path(index.php instead of the page itself) and $page->url() allowed the script to run from the page. Love the Kirby fix.

Thanks so much!

Btw Kirby provided a wrapper around curl, see Remote class in reference, more comfy to use