markdown 简单的联系表格在Kirby 3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 简单的联系表格在Kirby 3相关的知识,希望对你有一定的参考价值。

# Simple Contact-Form in Kirby 3

As a starting point I've used the latest Starterkit (Beta 4) combined with the core from the latest Nightly Build (18-08-06).

First, set [email](https://nnnnext.getkirby.com/docs/guide/emails)-properties in your config. I would recommend using a mail-service like Mailgun (via SMTP) because setting everything up on your own server is cumbersome, error-prone, and unsecure. But of course, it's up to you.

`site/config/config.php`:
```php
<?php

return [

  'debug'  => true,

	'email' => [

	  'transport' => [
	    'type' => 'smtp',
	    'port' => 465,
	    'ssl' => true,
	    'host' => 'smtp.server.com',
	    // If authentification is needed
      	    'auth' => true,
	    'username' => '...',
	    'password' => '...',
	  ],

	  'presets' => [
	    'contact-form' => [
	      'template'=> 'contact-form',
	      'from'    => 'no-reply@your-domain.com',
              // Replace this with your email
	      'to'    => 'you@your-mail.com',
	    ],
	  ]

  ]

];
```


Then we setup a `contact` [template](https://nnnnext.getkirby.com/docs/guide/templates/template-basics). Because that's already done in the Starterkit I've added a new section and put the actual form in a [snippet](https://nnnnext.getkirby.com/docs/guide/templates/snippets).

`site/templates/contact.php`:

```php
...

<section>

  <h2>Form</h2>

  <?php if(param('success')): ?>

    <!-- Success -->
    <p>Message successfully sent.</p>

  <?php elseif(isset($alerts) && $alerts): ?>

    <!-- Errors -->
    <p>Fix the following errors:
      <ul>
        <?php foreach($alerts as $alert): ?>
          <li><?= html($alert) ?></li>
        <?php endforeach ?>
      </ul>
    </p>

  <?php endif ?>

  <!-- Actual Form -->
  <?= snippet('contact-form') ?>

</section>

...
```

`site/snippets/contact-form.php`:
```php
<form method="post">

  <div>
    <label for="name">Name</label><br>
    <input type="text" id="name" name="name" required autofocus>
  </div>

  <div>
    <label for="email">E-Mail</label><br>
    <input type="email" id="email" name="email" required>
  </div>

  <div>
    <label for="message">Message</label><br>
    <textarea id="message" name="message" required></textarea>
  </div>

  <button type="submit" name="submit" value="submit">Submit</button>

</form>
```


With the current size of the form a snippet does not really make sense but with more fields, classes and wrappers it'll propably become a beast, so I would recommend using a snippet ;)

The actual logic of the form lives inside a [controller](https://nnnnext.getkirby.com/docs/guide/templates/controllers) which must be named exactly like our template (`contact`).

`site/controllers/contact.php`:
```php
<?php

return function($site, $pages, $page) {
  if(!get('submit')) return;

  $successUrl = url($page->url(), [ 'params' => 'success:true' ]);
  $alerts = null;

  // Gather form-data
  $form = kirby()->request()->data();

  // TODO
  // Validate form-data here..
  // Propably use `Kirby\Toolkit\V::input` here like suggested by @CelineDesign

  // Try to send the mail
  try {

    kirby()->email('contact-form', [
      'subject' => "Message by {$form['name']}",
      'replyTo' => $form['email'],
      'data' => [ 'form' => $form ]
    ]);

    go($successUrl);

  } catch (Exception $error) {
    return [ 'alerts' => [ $error ] ];
  }

};
```

And finally we need the actual email-templates which are going to be sent. We called it `contact-form` in the config so lets create two(!) files:

  * `site/emails/contact-form.html.php`
  * `site/emails/contact-form.text.php`

Within these templates you can write all the template-code you are used to and also use global objects like `site()` or `kirby()`.

`site/emails/contact-form.html.php`:
```php
<p>
  Name: <?= $form['name'] ?>
</p>

<p>
  E-Mail: <?= $form['email'] ?>
</p>

<p>
  Message: <?= $form['message'] ?>
</p>
```

`site/emails/contact-form.text.php`:
```php
Name: <?= $form['name'] ?>

E-Mail: <?= $form['email'] ?>

Message: <?= $form['message'] ?>
```

***


# What could be improved

* Add a simple Honeypot
* Add Form-Validation
* Use JavaScript for a loading animation when the form gets send
* Or make the form completely AJAXish
* Send a second mail as a confirmation for the user..
* Better form markup (for autocomplete and accessibility)

以上是关于markdown 简单的联系表格在Kirby 3的主要内容,如果未能解决你的问题,请参考以下文章

markdown 联系表格7

markdown Vue联系表格

php Kirby CMS - 结构域中的图像

text Kirby nginx配置

php kirby Cache Object单

用 Excel 生成和管理 Markdown 表格--转载