Radio Buttons in contact form

Hello ,
I have a problem with the radio buttons in the contact form.
The form doesn’t submit and I don’t get an error message.
Everything works without radio buttons.

When I fill out the form without selecting the radio button I get the following error message:

esc(): Argument #1 ($string) must be of type string, null given, called in on line 51

Line 51 in controller:

   try {
          $kirby->email([
              'template' => 'email',
              'from'     => 'yourcontactform@yourcompany.com',
              'replyTo'  => $data['email'],
              'to'       => 'you@yourcompany.com',
              'subject'  => esc($data['name']) . ' sent you a message from your contact form',
              'data'     => [
                  ...
           /* line 51 */ 'abschluss'  => esc($data['abschluss']), 
                 ...
              ]
          ]);

Controller

  <?php
  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 = [
              'name'  => get('name'),
              'abschluss' => get('abschluss'),
              'telefon' => get('telefon'),
              'email' => get('email'),
              'text'  => get('text')
          ];
  
          $rules = [
              'name'  => ['required', 'minLength' => 3],
              'email' => ['required', 'email'],
              'telefon'  => ['required', 'minLength' => 5],
              'abschluss' => ['in' => [['ja','nein']]],
          ];
  
          $messages = [
              'name'  => 'Bitte tragen Sie hier Ihren Namen ein!',
              'email' => 'Bitte tragen Sie hier eine gültige E-Mailadresse ein!',
              'telefon' => 'Bitte tragen Sie hier Ihre Telefonnummer ein!',
              
          ];
  
          // 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'     => 'yourcontactform@yourcompany.com',
                      'replyTo'  => $data['email'],
                      'to'       => 'you@yourcompany.com',
                      'subject'  => esc($data['name']) . ' sent you a message from your contact form',
                      'data'     => [
                          'name'   => esc($data['name']),
                          'abschluss'  => esc($data['abschluss']),
                          'email'   => esc($data['email']),
                          'telefon'   => esc($data['telefon']),
                          'text'   => esc($data['text']),
                      ]
                  ]);
  
              } 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 = [];
              }
          }
      }
  
      return [
          'alert'   => $alert,
          'data'    => $data ?? false,
          'success' => $success ?? false
      ];
  };

Form

...
 <div class="mb-24">
   <label for="abschluss">Mit Berufsabschluss? <abbr title="required">*</abbr></label>
     <p>
       <?php $value = $data['abschluss']?? '' ?>
         <input type="radio" name="abschluss" value="ja" <?php e($value=='ja', 'checked')?>>Ja</input>
         <input type="radio" name="abschluss" value="nein" <?php e($value=='nein', 'checked')?>>Nein</input>
         <?= isset($alert['abschluss']) ? '<span class="alert error">' . html($alert['abschluss']) . '</span>' : '' ?>
      </p>
  </div>
 ... 

E-Mail-Template

<p>Name: <?= $name ?></p>
<p>Abschluss: <?= $abschluss ?></p>
<p>Telefon: <?= $telefon ?></p>
<p>E-Mail: <?= $email ?></p>
<p><?= $text ?></p>

Thank you for your help.

This means the the esc() method requires a string as input. If you don’t submit a value and $data['abschluss'] is therefor null and not a string, you get the error.

The right way to do this would be

esc($data['abschluss'] ?? '')

So my controller looks like:

...
 try {
                $kirby->email([
                    'template' => 'email',
                    'from'     => 'yourcontactform@yourcompany.com',
                    'replyTo'  => $data['email'],
                    'to'       => 'you@yourcompany.com',
                    'subject'  => esc($data['name']) . ' sent you a message from your contact form',
                    'data'     => [
                        ...
                        'abschluss'  => esc($data['abschluss'] ?? ''),
                       ...
                    ]
                ]);
...

If I choose “yes” or “no” I get the error message

The form could not be sent: Undefined variable $ja

or

The form could not be sent: Undefined variable $nein

If I don’t choose an option:

The form could not be sent: Undefined variable $

Where does this error originate?

It would help if you post the complete template.

Unfortunately I don’t know exactly where this error comes from! :pensive:

Template:

<?php if($success): ?>
 
<div class="alert success">
  <p><?= $success ?></p>
</div>
  
<?php else: ?>
 
<?php if (isset($alert['error'])): ?>
  <div><?= $alert['error'] ?></div>
<?php endif ?>
 
<form method="post" action="<?= $page->url() ?>">
    
<div class="honeypot">
   <label for="website">Website <abbr title="required">*</abbr></label>
   <input type="url" id="website" name="website" tabindex="-1">
</div>
    
<div class="mb-24">
  <label for="name"><small>Name <abbr title="required">*</abbr></small></label>
  <input class="block w-full" type="text" id="name" name="name" value="<?= esc($data['name'] ?? '', 'attr') ?>" required>
    <?= isset($alert['name']) ? '<span class="alert error">' . esc($alert['name']) . '</span>' : '' ?>
</div>
    
<div class="mb-24">
  <label for="abschluss">Mit Berufsabschluss? <abbr title="required">*</abbr></label>
  <p>
    <?php $value = $data['abschluss']?? '' ?>
    <input type="radio" name="abschluss" value="ja" <?php e($value=='ja', 'checked')?>>Ja</input>
    <input type="radio" name="abschluss" value="nein" <?php e($value=='nein', 'checked')?>>Nein</input>
    <?= isset($alert['abschluss']) ? '<span class="alert error">' . html($alert['abschluss']) . '</span>' : '' ?>
  </p>
</div>
    
<div class="mb-24">
  <label for="telefon"><small>Telefon <abbr title="required">*</abbr></small></label>
  <input class="block w-full" type="text" id="telefon" name="telefon" value="<?= esc($data['telefon'] ?? '', 'attr') ?>" required>
  <?= isset($alert['telefon']) ? '<span class="alert error">' . esc($alert['telefon']) . '</span>' : '' ?>
</div>

<div class="mb-24">
  <label for="email"><small>Email <abbr title="required">*</abbr></small></label>
  <input class="block w-full"  type="email" id="email" name="email" value="<?= esc($data['email'] ?? '', 'attr') ?>" required>
  <?= isset($alert['email']) ? '<span class="alert error">' . esc($alert['email']) . '</span>' : '' ?>
</div>
 
<div class="mb-24">
  <label for="text"><small>Text</small></label>
  <textarea id="text" name="text" class="block w-full">
    <?= esc($data['text'] ?? '') ?>
   </textarea>
   <?= isset($alert['text']) ? '<span class="alert error">' . esc($alert['text']) . '</span>' : '' ?>
</div>

<label class="mb-24 flex">
<input type="checkbox" id="datenschutz" name="datenschutz" value="<?= esc($data['datenschutz'] ?? '', 'attr') ?>" required>
    <?= isset($alert['datenschutz']) ? '<span class="alert error">' . esc($alert['datenschutz']) . '</span>' : '' ?>
    <p class="ml-24">Datenschutztext...</p>
</label>
    
<input type="submit" name="submit" value="Submit">  </form>
<?php endif ?>

What if you comment the $kirby->email() code snippet in the controller?

When I do this in controller:

// 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'     => 'yourcontactform@yourcompany.com',
                    'replyTo'  => $data['email'],
                    'to'       => 'you@yourcompany.com',
                    'subject'  => esc($data['name']) . ' sent you a message from your contact form',
                    'data'     => [
                        'name'   => esc($data['name']),
                        'abschluss'  => esc($data['abschluss'] ?? ''),
                        'email'   => esc($data['email']),
                        'telefon'   => esc($data['telefon']),
                        'text'   => esc($data['text']),
                    ]
                ]);
               */

nothing happens. Only the page is reloaded and the data are still in the form.

Hm, you should get your success message in that case. If I copy your template and controller into a Starterkit, without sending email it works fine (haven’t tested with sending data, because I didn’t want to add any transport data first).

Are you sending your form to the right target, i.e. do have a page with a contact.txt, with a controller contact.php and a template contact.php? Or named differently, but same logic?

I found another error in the email template! :roll_eyes: soooo stupid!
It looks like it’s working now.

Thank you for your time and great support!!! :+1: :clap: