Sending data to structure via e-mail form

Hi
My form is working very well : sending two emails, one for the client and another to the sender for confirmation.
I would like to know how to send data into a structure field on a specific page ?

I have built a page “commandes” with blueprint :

fields:
  commandes:
    label: Commandes
    type: structure
    fields:
      titre:
        label: Titre
        type: text
      name:
        label: Nom
        type: text
      prenom:
        label: Prénom
        type: text
      email:
        label: E-mail
        type: text
      telephone:
        label: Téléphone
        type: text
      paiement:
        label: Paiement
        type: text
      via:
        label: Via
        type: text

And my controller form looks like this :

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

   $alert = null;

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

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

      $data = [
         'titre' => $page->title(),
         'date' => $page->date(),
         'prix' => $page->prix(),
         'lieu' => $page->lieu(),
         'name'  => get('name'),
         'prenom'  => get('prenom'),
         'email' => get('email'),
         'telephone' => get('telephone'),
         'paiement' => get('paiement'),
         'via' => get('via')
      ];

      $rules = [
         'name'  => ['required', 'min' => 3],
         'prenom'  => ['required', 'min' => 3],
         'email' => ['required', 'email'],
         'paiement'  => ['in' => [['virement', 'cash']]],
         'via'  => ['in' => [['Facebook','Internet','Papier']]],
      ];

      $messages = [
         'name'  => 'Please enter a valid name',
         'prenom' => 'Please enter a valid email address',
         'email' => 'Please enter a valid email address',
         'paiement' => 'Please enter a valid email address',
         'places' => 'Please enter a valid email address',
         'via' => '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 {
            $kirby->email([
               'template' => 'email',
               'from'     => 'no-reply@yop.be',
               'replyTo'  => $data['email'],
               'to'       => 'me@yahoo.fr',
               'subject'  => 'Une réservation à "' . esc($data['titre']) . '"',
               'data'     => [
                  'titre' => esc($data['titre']),
                  'date' => esc($data['date']),
                  'prix' => esc($data['prix']),
                  'lieu' => esc($data['lieu']),
                  'name'   => esc($data['name']),
                  'prenom' => esc($data['prenom']),
                  'telephone' => esc($data['telephone']),
                  'email' => esc($data['email']),
                  'paiement' => esc($data['paiement']),
                  'via' => esc($data['via'])

               ]
            ]);
            $kirby->email([
               'template' => 'sender',
               'from'     => 'no-reply@yop.be',
               'replyTo'  => $data['email'],
               'to'       => $data['email'],
               'subject'  => 'Votre inscription à "' . esc($data['titre']) . '"',
               'data'     => [
                  'titre' => esc($data['titre']),
                  'date' => esc($data['date']),
                  'prix' => esc($data['prix']),
                  'lieu' => esc($data['lieu']),
                  'titre' => esc($data['titre']),
                  'name'   => esc($data['name']),
                  'prenom' => esc($data['prenom']),
                  'email' => esc($data['email']),
                  'paiement' => esc($data['paiement']),
                  'via' => esc($data['via'])

               ]
            ]);
            
            

         } catch (Exception $error) {
            $alert['error'] = "Le formulaire n’a pas pu être envoyé";
         }

         // no exception occured, let's send a success message
         if (empty($alert) === true) {
            go(url($pages->find('merci')));
            $data = [];
         }
      }
   }

   return [
      'alert'   => $alert,
      'data'    => $data ?? false,
      'success' => $success ?? false
   ];
};

maybe it helps: https://getkirby.com/docs/reference/objects/page/update

Basically like this: Structure field update with $page->update().

Tried many integrations of your script after kirby->email(). The email is sended but the structure is not written. I don’t really know how to implement it.

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

   $alert = null;

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

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

      $data = [
         'titre' => $page->title(),
         'date' => $page->date(),
         'prix' => $page->prix(),
         'lieu' => $page->lieu(),
         'name'  => get('name'),
         'prenom'  => get('prenom'),
         'email' => get('email'),
         'telephone' => get('telephone'),
         'paiement' => get('paiement'),
         'via' => get('via')
      ];

      $rules = [
         'name'  => ['required', 'min' => 3],
         'prenom'  => ['required', 'min' => 3],
         'email' => ['required', 'email'],
         'paiement'  => ['in' => [['virement', 'cash']]],
         'via'  => ['in' => [['Facebook','Internet','Papier']]],
      ];

      $messages = [
         'name'  => 'Please enter a valid name',
         'prenom' => 'Please enter a valid email address',
         'email' => 'Please enter a valid email address',
         'paiement' => 'Please enter a valid email address',
         'places' => 'Please enter a valid email address',
         'via' => '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 {
            $kirby->email([
               'template' => 'email',
               'from'     => 'no-reply@yop.be',
               'replyTo'  => $data['email'],
               'to'       => 'me@yahoo.fr',
               'subject'  => 'Une réservation à "' . esc($data['titre']) . '"',
               'data'     => [
                  'titre' => esc($data['titre']),
                  'date' => esc($data['date']),
                  'prix' => esc($data['prix']),
                  'lieu' => esc($data['lieu']),
                  'name'   => esc($data['name']),
                  'prenom' => esc($data['prenom']),
                  'telephone' => esc($data['telephone']),
                  'email' => esc($data['email']),
                  'paiement' => esc($data['paiement']),
                  'via' => esc($data['via'])

               ]
            ]);
            $kirby->email([
               'template' => 'sender',
               'from'     => 'no-reply@yop.be',
               'replyTo'  => $data['email'],
               'to'       => $data['email'],
               'subject'  => 'Votre inscription à "' . esc($data['titre']) . '"',
               'data'     => [
                  'titre' => esc($data['titre']),
                  'date' => esc($data['date']),
                  'prix' => esc($data['prix']),
                  'lieu' => esc($data['lieu']),
                  'titre' => esc($data['titre']),
                  'name'   => esc($data['name']),
                  'prenom' => esc($data['prenom']),
                  'email' => esc($data['email']),
                  'paiement' => esc($data['paiement']),
                  'via' => esc($data['via'])

               ]
            ]);
            
             function addToStructure($page, $field, $data = array()){
               $fieldData = $pages->find('commandes')->commandes()->yaml();
               $fieldData[] = $data;
               $fieldData = yaml::encode($fieldData);
               try {
                  $pages->find('commandes')->update(array($field => $fieldData));
                  return true;
               } catch(Exception $e) {
                  return $e->getMessage();
               }
            } 

         } catch (Exception $error) {
            $alert['error'] = "Le formulaire n’a pas pu être envoyé";
         }

         // no exception occured, let's send a success message
         if (empty($alert) === true) {
            go(url($pages->find('merci')));
            $data = [];
         }
      }
   }

   return [
      'alert'   => $alert,
      'data'    => $data ?? false,
      'success' => $success ?? false
   ];
};

is just defined but never called as far as i can tell.

You can see how to call the function with parameters here: Add to Structure in specific language

Thanks it’s working !

function addToStructure($page, $field, $data = array()){
               $fieldData = page($page)->$field()->yaml();
               $fieldData[] = $data;
               $fieldData = yaml::encode($fieldData);
               try {
                  page($page)->update(array($field => $fieldData));
               } catch(Exception $e) {
                  return $e->getMessage();
               }
            }

            addToStructure($pages->find('commandes'), 'commandes', [
               'titre' => esc($data['titre']),
               'name'   => esc($data['name']),
               'prenom' => esc($data['prenom']),
               'telephone' => esc($data['telephone']),
               'email' => esc($data['email']),
               'paiement' => esc($data['paiement']),
               'places' => esc($data['places']),
               'via' => esc($data['via'])
            ]);
1 Like

I’d move the function to a plugin, so it doesn’t clutter your controller.