Send form to API

Im trying to send form data from a simple form to an API based on this example and Remote::request. Contact form | Kirby CMS

For some reason it just keeps reloading the page and doesnt send the form to API. I know the Remote:request part works fine since putting it in a template on its own is succesful

What am i doing wrong? i think probably its not causing an exception properly or something since thats part of the email sending logic from the cookbook, but not really sure how to correct it.

Here is my entire controller:

<?php

return function ($page, $kirby, $site) {

  // SEO
  $seo = $kirby->controller('seo' , compact('page', 'site', 'kirby'));

  // Override Meta Title
  $metatitle = $site->title();

  $theme = $page->theme()->or('normal');

  $alert = null;

  if($kirby->request()->is('POST') && get('submit')) {

      // check the honeypot
      if(empty(get('website')) === false) {
          go($page->url());
          exit;
      }

      $data = [
        
          'email' => get('email'),
      ];

      $rules = [
        
          'email' => ['required', 'email'],
        
      ];

      $messages = [
   
          'email' => 'Please enter a valid email address',
      
      ];

      // some of the data is invalid
      if($invalid = invalid($data, $rules, $messages)) {
          $alert = $invalid;

          // the data is fine, let's send the email
      } else {
          try {


              $url = 'XXX';
              $data = [
                'api_key' => 'XXX',
                'email_address' => $data['email'],
                'fields' => ['FirstName' => 'null', 'LastName' => 'null'],
                'SubscribeDate' => '2024-10-09T10:28:51+00:00',
                'status' => 'SUBSCRIBED'
              ];
              $options = [
                  'headers' => [     
                      'Content-Type: application/json'
                  ],
                  'method'  => 'POST',
                  'data'    => json_encode($data)
              ];
              $response = Remote::request($url, $options);



          } catch (Exception $error) {
              if(option('debug')):
                  $alert['error'] = 'The form could not be sent: <strong>' . $error->getMessage() . '</strong>';
              else:
                  $alert['error'] = 'The form could not be sent!';
              endif;
          }

          // no exception occurred, let's send a success message
          if (empty($alert) === true) {
              $success = 'Your message has been sent, thank you. We will get back to you soon!';
              $data = [];
          }
      }
  }




    $alert   = $alert;
    $data    = $data ?? false;
    $success = $success ?? false;

    $pdata = compact('metatitle', 'theme', 'alert', 'data', 'success' );

    return array_merge($seo, $pdata);

};

Try to dump something before and after your if statement. My guess is that you probably don’t get into the if statement. Might also help to remove the && get('submit') part.

Hrmm… i did try dumping $data but its empty, even after hitting the submit button.

Aaaaha! That did it :slight_smile: thanks @texnixe