Links always lead to home

Hey,
I want to set up a contact form on a customers site. But when I click send, I will always get directed to the index site. Same with, when I mistype the URL. So I guess the contact form cant send, because the htaccess or something else is directing the user to the index, instead of sending the form.
htaccess:


# Kirby .htaccess
<IfModule mod_rewrite.c>

RewriteEngine on

# make sure to set the RewriteBase correctly
RewriteBase /

# block text files in the content folder from being accessed directly
RewriteRule ^content/(.*)\.(txt|md|mdown)$ index.php [L]

# block all files in the site folder from being accessed directly
# except for requests to plugin assets files
#RewriteRule ^assets/plugins/([a-zA-Z0-9\.\-_%=]+)/(.*)$ site/plugins/$1/assets/$2 [L,N]
#RewriteCond $1 !^plugins/[a-zA-Z0-9\.\-_%=]+/assets/.*
RewriteRule ^site/(.*) index.php [L]

# block direct access to kirby and the panel sources
RewriteRule ^(kirby|panel\/app|panel\/tests)/(.*) index.php [L]

# make panel links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^panel/(.*) panel/index.php [L]

# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

</IfModule>

controller:

<?php

return function($kirby) {

  $alert = null;

  if(get('submit')) {

    $data = array(
      'firma'  => get('firma'),
      'sex' => get('sex'),
      'vorname' => get('vorname'),
      'nachname' => get('nachname'),
      'email' => get('email'),
      'tel' => get('tel'),
      'title' => get('title'),
      'date' => get('date'),
      'message'  => get('message')
    );

    $rules = array(
      'firma'  => array('required'),
      'sex' => array('required'),
      'vorname' => array('required'),
      'nachname' => array('required'),
      'email' => array('required'),
      'tel' => array('required'),
      'title' => array('required'),
      'date' => array('required'),
      'message'  => array('required', 'min' => 3, 'max' => 3000),
    );

    $messages = array(
      'firma'  => 'Please enter a valid firma',
      'sex'  => 'Please enter a valid',
      'vorname'  => 'Please enter a valid',
      'nachname'  => 'Please enter a valid',
      'email' => 'Please enter a valid email address',
      'tel'  => 'Please enter a valid',
      'title'  => 'Please enter a valid',
      'date'  => 'Please enter a valid',
      'message'  => 'Please enter a text between 3 and 3000 characters'
    );

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

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

      // create the body from a simple snippet
      try {
        $kirby->email([
          'from' => 'mymail@web.de',
          'replyTo' => 'no-reply@supercompany.com',
          'to' => 'mymail@web.de',
          'cc' => 'anotherone@gmail.com',
          'bcc' => 'secret@gmail.com',
          'subject' => 'Welcome!',
          'body'=> 'Testnachricht!',
        ]);
      } catch (Exception $error) {
        echo $error;
      }

    }

  }

  return compact('alert');

};

form:

<form class="modal-content" method="post" action="event.php">

    <div class="modal__container">
      <h1>Anmeldung fĂĽr</h1>
      <h2><?= $page->title() ?></h2>
      <div class="modal__inputs__group__wrapper">
        <?php if($alert): ?>
        <div class="alert">
          <ul>
            <?php foreach($alert as $message): ?>
            <li><?php echo html($message) ?></li>
            <?php endforeach ?>
          </ul>
        </div>
        <?php endif ?>

        <div class="modal__inputs__group">

          <div class="input__field">
            <input type="text" id="firma" name="firma" required><br>
            <label for="firma">Firma*</label><br>
          </div>
          <div class="input__field">
            <select name="sex" id="sex">
              <option value="Herr">Herr</option>
              <option value="Frau">Frau</option>
            </select>
            <label for="sex">Anrede*</label><br>
          </div>
          <div class="input__field">
            <input type="text" id="vorname" name="vorname"><br>
            <label for="vorname">Vorname*</label><br>
          </div>
          <div class="input__field">
            <input type="text" id="nachname" name="nachname"><br>
            <label for="nachname">Nachname*</label><br>
          </div>
        </div>
        <div class="modal__inputs__group">
          <div class="input__field">
            <input type="email" id="email" name="email"><br>
            <label for="email">E-Mail*</label><br>
          </div>
          <div class="input__field">
            <input type="tel" id="tel" name="tel"><br>
            <label for="tel">Telefon/Mobil*</label><br>
          </div>
          <div class="input__field">
            <input disabled type="text" id="title" name="title" value="<?= $page->title() ?>"><br>
            <label for="title">Titel der Schulung*</label><br>
          </div>



          <div class="input__field">
            <input type="text" id="date" name="date" value=""> <br>
            <label for="date">Schulungsdatum*</label><br>
          </div>

        </div>
        <div class="modal__inputs__textarea">
          <p>Raum fĂĽr Anmerkungen<span> (optional)</span></p>
          <textarea id="message" name="message" rows="8" cols="80"></textarea>
          <p class="text-align-end">
            <!-- <a href=""> -->
              <input class="senden-button" type="submit" name="submit" value="Senden">
            <!-- </a> -->
          </p>

        </div>
      </div>


      <div class="close">

      </div>


    </div>

  </form>

Hope you can help me. Thanks in advance :slight_smile:

You action should not point to a script name, but to the page url that handles the form.

I tried action=“event_handler.php” and moved the “controller” to the same directory but it doesn’t work :confused:

it needs to be the path to the page.

action="/event"

That is not a url! You cannot directly call a php script. Assuming you have created a page to which the controller belongs, pass the url of that page to the action attribute. And the controller has to stay in the /site/controllers folder.

E.g.

action="<?= page('contact')->url() ?>"

Or whatever the page is

The page is always a different subpage of “akademie”.
Can’t I just do <?= $page->url() ?>
This doesnt work though :confused:

As long as the subpages have the same template, the controller should kick in.

The template is also event. When I do <?= page('event')->url() ?> I get Call to a member function url() on null

The template has nothing to do with the page slug. You don’t seem to have a page called event.

Maybe you can post your page folder structure together with the templates/controllers used for the pages. And indicate in which template you use the form.

root
  content
    3_akademie
  site
    controllers
      event.php
    templates
      event.php

I strongly appreciate the fast answers by the way :slight_smile:

But where are the forms? Which template is used by for the forms?

the form is inside the templates → event.php

If that is the case, then using

<form class="modal-content" method="post" action="<?= $page->url() ?>">

Should be the way to go.

But your form is somehow incomplete, because you don’t return the data in your value attributes in case you have to return an alert. See form example here: Email contact form | Kirby CMS

Are you actually using Kirby 3? You .htaccess file looks outdated, as if it was from Kirby 2?

If you are on Kirby 3, use the .htaccess from here:

1 Like

I will try this tomorrow, thank you :slight_smile:
Also thanks for the htaccess, this looks better than the one from the customer. Yes its kirby3

Okay this is weird. When I submit the form, the success message appears “Your message has been sent…” but there is nothing in my inbox. MY code looks like this now:

controller: event.php

<?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 = [
            'firma'  => get('firma'),
            'vorname'  => get('vorname'),
            'nachname'  => get('nachname'),
            'email' => get('email'),
            'tel' => get('tel'),
            'title' => get('title'),
            'date' => get('date'),
            'message'  => get('message')
        ];

        $rules = [
            'firma'  => ['required'],
            'vorname'  => ['required'],
            'nachname'  => ['required'],
            'email' => ['required', 'email'],
            'tel'  => ['required'],
            'title'  => ['required'],
            'date'  => ['required'],
            'message'  => ['required', 'minLength' => 3, 'maxLength' => 3000],
        ];

        $messages = [
            'firma'  => 'Please enter a valid Firma',
            'email' => 'Please enter a valid email address',
            'message'  => 'Please enter a text between 3 and 3000 characters'
        ];

        // 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'       => 'actually.mymail@web.de',
                    'subject'  => esc($data['firma']) . ' sent you a message from your contact form',
                    'data'     => [
                        'text'   => esc($data['message']),
                        'sender' => esc($data['firma'])
                    ]
                ]);

            } 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
    ];
};

template: event.php

<?php
snippet('header');
snippet('organisms/spotlights');
snippet('organisms/main-header');

snippet('organisms/main-event', ['data' => $page]);

foreach ($page->ces()->toBuilderBlocks() as $ce) {
    snippet('organisms/' . $ce->_key(), array('data' => $ce));
}
?>
<!-- <div class="ce-event__jetzt__anmelden">
  <p>
    <a onclick="document.getElementById('<?= $page ?>').style.display='block'" href="#">Jetzt anmelden</a>
  </p>
</div> -->

<a onclick="document.getElementById('<?= $page ?>').style.display='block'" href="#">X</a>

<?php if($success): ?>
<div class="alert success">
    <p><?= $success ?></p>
</div>
<?php else: ?>
<?php if (isset($alert['error'])): ?>
    <div><?= $alert['error'] ?></div>
<?php endif ?>

<div id="<?= $page ?>" class="modal">

  <form class="modal-content" method="post" action="<?= $page->url() ?>">

    <div class="modal__container">
      <h1>Anmeldung fĂĽr</h1>
      <h2><?= $page->title() ?></h2>
      <div class="modal__inputs__group__wrapper">

        <div class="honeypot">
            <label for="website">Website <abbr title="required">*</abbr></label>
            <input type="url" id="website" name="website" tabindex="-1">
        </div>



        <div class="modal__inputs__group">

          <div class="input__field">
            <input type="text" id="firma" name="firma" value="<?= esc($data['firma'] ?? '', 'attr') ?>" required><br>
            <label for="firma">Firma*</label><br>
            <?= isset($alert['firma']) ? '<span class="alert error">' . esc($alert['firma']) . '</span>' : '' ?>
          </div>
          <div class="input__field">
            <select name="sex" id="sex">
              <option value="Herr">Herr</option>
              <option value="Frau">Frau</option>
            </select>
            <label for="sex">Anrede*</label><br>
          </div>
          <div class="input__field">
            <input type="text" id="vorname" name="vorname" value="<?= esc($data['vorname'] ?? '', 'attr') ?>" required><br>
            <label for="vorname">Vorname*</label><br>
            <?= isset($alert['vorname']) ? '<span class="alert error">' . esc($alert['vorname']) . '</span>' : '' ?>
          </div>
          <div class="input__field">
            <input type="text" id="nachname" name="nachname" value="<?= esc($data['vorname'] ?? '', 'attr') ?>" required><br>
            <label for="nachname">Nachname*</label><br>
            <?= isset($alert['nachname']) ? '<span class="alert error">' . esc($alert['nachname']) . '</span>' : '' ?>
          </div>
        </div>
        <div class="modal__inputs__group">
          <div class="input__field">
            <input type="email" id="email" name="email" value="<?= esc($data['email'] ?? '', 'attr') ?>" required><br>
            <label for="email">E-Mail*</label><br>
            <?= isset($alert['email']) ? '<span class="alert error">' . esc($alert['email']) . '</span>' : '' ?>
          </div>
          <div class="input__field">
            <input type="tel" id="tel" name="tel" value="<?= esc($data['tel'] ?? '', 'attr') ?>" required><br>
            <label for="tel">Telefon/Mobil*</label><br>
            <?= isset($alert['tel']) ? '<span class="alert error">' . esc($alert['tel']) . '</span>' : '' ?>
          </div>
          <div class="input__field">
            <input type="text" id="title" name="title"  value="<?= esc($data['title'] ?? '', 'attr') ?>" required><br>
            <label for="title">Titel der Schulung*</label><br>
            <?= isset($alert['title']) ? '<span class="alert error">' . esc($alert['title']) . '</span>' : '' ?>
          </div>



          <div class="input__field">
            <input type="text" id="date" name="date" value="<?= esc($data['date'] ?? '', 'attr') ?>" required> <br>
            <label for="date">Schulungsdatum*</label><br>
            <?= isset($alert['date']) ? '<span class="alert error">' . esc($alert['date']) . '</span>' : '' ?>
          </div>

        </div>
        <div class="modal__inputs__textarea">
          <p>Raum fĂĽr Anmerkungen<span> (optional)</span></p>
          <textarea id="message" name="message" rows="8" cols="80">
            <?= esc($data['message'] ?? '') ?>
          </textarea>
          <?= isset($alert['message']) ? '<span class="alert error">' . esc($alert['message']) . '</span>' : '' ?>
          <p class="text-align-end">
            <!-- <a href=""> -->
              <input class="senden-button" type="submit" name="submit" value="Senden">
            <!-- </a> -->
          </p>

        </div>
      </div>


      <div class="close">
        <span onclick="document.getElementById('<?= $page ?>').style.display='none'"  title="Fenster schlieĂźen">
          <!-- <img src="assets\images\dropdown_close_icon.svg" alt=""> -->
          <svg  version="1.1" id="Ebene_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
             viewBox="0 0 15.4 15.3" style="enable-background:new 0 0 15.4 15.3;" xml:space="preserve">
          <style type="text/css">
            .st0{fill:none;stroke:#184285;stroke-width:3;}
          </style>
          <g id="Gruppe_106" transform="translate(-425.281 -964.887)">
            <path id="Pfad_18_3_" class="st0" d="M425.6,983.2"/>
            <path id="Pfad_124_2_" class="st0" d="M425.6,996.4"/>
            <path id="Pfad_124_1_" class="st0" d="M438,996.4"/>
          </g>
          <g id="Gruppe_106_1_" transform="translate(-425.281 -964.887)">
            <path id="Pfad_18" class="st0" d="M426.5,966l6.2,6.6l6.2-6.6"/>
            <path id="Pfad_124" class="st0" d="M438.9,979.2l-6.2-6.6l-6.2,6.6"/>
          </g>
          </svg>
        </span>
      </div>


    </div>

  </form>
  <?php endif ?>
</div>

<script>
// Get the modal
var modal = document.getElementById('id01');

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
</script>

<?php
snippet('footer');

templates/email: email.php

Hello Company,

<?= $text ?>

<?= $sender ?>

templates/email: email.html.php

Hello Company,

<p><?= $text ?></p>

<p>Best,</p>
<p><?= $sender ?></p>

Are you trying to send the email from localhost or from a remote server? Have you checked your spam folder?

live server. nothing in spam

What I’d do is first test locally with a tool like Mailhog, to see if the setup works as expected:

Then once you know it should work, start debugging on the remove server. Often if mail doesn’t work, it is some issue with blocked mail senders. Then try sending via an external service like MailChimp and the like.