ttskch/contact-form 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ttskch/contact-form

Composer 安装命令:

composer require ttskch/contact-form

包简介

PHP utility classes to implement general contact form

README 文档

README

Travis (.com) branch Latest Stable Version Total Downloads

PHP utility classes to implement general contact form (also with confirmation view). It's maybe useful to build contact form on WordPress or pure PHP site.

Requirements

  • PHP 5.4+
  • Configuring date.timezone in php.ini

Supported features

  • Csrf protection with session and hidden input tag
  • Server side validation for submitted values
  • Short hands to print submitted value itself, validation errors, "selected" option, "checked" option
  • Attaching files and handle them easily in the same session
  • Sending email which contains submissions information easily

Installation

$ composer require ttskch/contact-form

or

$ git clone git@github.com:ttskch/contact-form.git
$ cd contact-form
$ composer install --no-dev

# If your web site is not composer-friendly, upload whole "contact-form" directory by hand.

Usage

<?php
// index.php

require_once '/path/to/contact-form/vendor/autoload.php';

$cf = new \Ttskch\ContactForm\ContactForm();

// validation targets ("required" and "email" are supported)
$requiredKeys = ['Name', 'Email'];
$emailKeys = ['Email'];

// after posted, validate csrf and submissions and redirect to next page
$cf->validateAndRedirectAfterSelfPosted('./confirm.php', $requiredKeys, $emailKeys);
?>

<!-- post to self -->
<form action="" method="post">
    
    <!-- put hidden input tag for csrf token -->
    <?= $cf->csrfHiddenInput(); ?>

    <!-- text field with submitted value if exists -->
    <input type="text" name="Name" value="<?= $cf->present('Name'); ?>" required autofocus>
    <!-- show error if exists -->
    <p><?= $cf->presentError('Name'); ?></p>
    
    <!-- text field with submitted value if exists -->
    <input type="email" name="Email" value="<?= $cf->present('Email'); ?>" required>
    <!-- show error if exists -->
    <p><?= $cf->presentError('Email'); ?></p>
    
    <!-- selector field with selection submitted or default option -->
    <select class="form-control" name="Gender">
        <option value="Male" <?= $cf->presentSelected('Gender', 'Male', $default = true); ?>>Male</option>
        <option value="Female" <?= $cf->presentSelected('Gender', 'Female'); ?>>Female</option>
        <option value="Other" <?= $cf->presentSelected('Gender', 'Other'); ?>>Other</option>
    </select>

    <button type="submit">Confirm</button>
</form>
<?php
// confirm.php

require_once '/path/to/contact-form/vendor/autoload.php';

$cf = new \Ttskch\ContactForm\ContactForm();

// redirect to top page if requested without submissions
$cf->rejectAccessWithoutSubmissions('./index.php');

// after posted, validate csrf and redirect to next page
$cf->validateAndRedirectAfterSelfPosted('./thanks.php');
?>

<!-- post to self -->
<form action="" method="post">

    <!-- put hidden input tag for csrf token -->
    <?= $cf->csrfHiddenInput(); ?>
    
    <!-- show submitted values -->
    <p><?= $cf->present('Name'); ?></p>
    <p><?= $cf->present('Email'); ?></p>
    <p><?= $cf->present('Gender'); ?></p>

    <button type="submit">Send</button>
    
    <!-- can back to index.php and re-edit inputs -->
    <a href="javascript:history.back();">Back</a>

</form>
<?php
// thanks.php

require_once '/path/to/contact-form/vendor/autoload.php';

$cf = new \Ttskch\ContactForm\ContactForm();

// redirect to top page if requested without submissions
$cf->rejectAccessWithoutSubmissions('./index.php');

$template = <<<EOT
----------------------------------------------------------------------
Name: %s
----------------------------------------------------------------------
Email: %s
----------------------------------------------------------------------
Gender: %s
----------------------------------------------------------------------
EOT;

$body = vsprintf($template, [
    $cf->present('Name', false),
    $cf->present('Email', false),
    $cf->present('Gender', false),
]);

$cf->sendEmail(
    'you@email.com',  // to
    'from@email.com', // from
    'Your Name',      // from name
    'Got inquiry',    // subject
    $body             // body
);

// clear submissions after sending email
// by this, if users reload thanks.php after sending email they will be redirected to index.php 
$cf->clearSubmissions();
?>

<p>Form is successfully submitted!</p>

See demo code or run it on your local to learn more :)

Precaution

Because this utility calls header() function to redirect, you must execute instantiation and some methods before any actual output is sent.

统计信息

  • 总下载量: 65
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固