Creating structure field entries from frontend works but …

Hey!
My goal is to build a counter for the amount of form submissions to an external form endpoint. So I came up the idea to write down each submit in a file and somebody linked to Creating pages from frontend | Kirby CMS but with the hint to do this with a strucured field instead. So I tinkered down the origin example and my testing code below is working. But if I add a value to the form’s action attribut, then the form itself works but there happens no entry in kirby anymore.

How do I have to solve this situatiuon?

This is my controller:

<?php
return function ($kirby, $page) {
  if ($kirby->request()->is('POST') && get('register')) {
    $data = [
    'name'  => get('name'),
    ];
    $kirby->impersonate('kirby');
    function addToStructure($page, $field, $data = array()){
      $fieldData = page($page)->$field()->yaml();
      $fieldData[] = $data;
      $fieldData = yaml::encode($fieldData);
      try {
        page($page)->update(array($field => $fieldData),'de');
      } catch(Exception $e) {
        return $e->getMessage();
      }
    }
    addToStructure($page, 'namen',[
      'name' => get('name'),
    ]);
  }
};

and this my template:

<form action="#"
    method="POST"
    id="form">
  <label for="name">Name </label>
  <input type="text" id="name" name="name" value="<?= $data['name'] ?? null ?>" required/>
  <input type="submit" name="register" value="Senden" />
  <div id="form-message"></div>
</form>

action attribute should go the page url…

Yes. I know. But I need the form to make an entry and send at the same time.

Where is the form handling done? That’s where you would have to also handle storing the entries when the form is successfully filled.

I use usebasin.com for forms. They offer an API. It seems like doing it this way, is more realistic. I’ll come back for this topic later :wink:

But as I understand you right. There is no way, sending a form to a url and entering content to my structured field at the same time?

If I see it right, the form is directly sent to an external endpoint, rather then you actively sending it there.

However, you could intercept this with a piece of JavaScript, or use their API, rather than the basic setup.

As an afterhthought: Why don’t you get this information from the form provider (e.g. via their API) when needed instead of storing this value on submission?

Simply put, set the form action to the page URL as described in the Kirby doc. Solve the form action to the endpoint via javascript. Did I understand that correctly?

Why not work with the API? Because working with APIs is not in my repertoire. But I will follow this approach and get someone to help me.

Thanks a lot for thinking along.

That’s a misunderstanding, but I can’t elaborate right now.

That’s probably the best.