teknicode/bootstrap-forms

PHP 类,用于生成具有 Bootstrap 4 样式的 HTML 表单。内置的表单捕获类可以通过电子邮件、短信或 MySQLi 发送表单。包括 Google ReCaptcha。

2.6.0 2018-10-14 17:08 UTC

This package is not auto-updated.

Last update: 2024-09-23 22:17:43 UTC


README

这个简单的类可以快速生成具有 Bootstrap 样式的 HTML 表单。

安装

composer require teknicode/bootstrap-forms

使用

require("src/Form.php");
use Teknicode\Form\Form;

$form = new Form();

现在您可以生成表单了。打开表单,传入一个包含要包含在表单标签中的属性的数组。如果您不提供 action 属性,表单将提交到其当前 URL。

$form->open([
    "id" => "my_form",
    "action" => "my url"
]);

输出 <form action="my url" id="my_form">

向表单添加输入。在 input(1) 中的 int 定义了输入的宽度,根据 Bootstraps 网格系统。例如,input(6) 将输入宽度设置为 col-md-6

$form->input(1)->set([
    "name"=>"my_first_input",
    "type"=>"text",
    "value" => "The current value of the input",
    "label" => "My First Input"
]);

输出 <input type="text" name="my_first_input" value="The current value of the input"/> 包裹在标签内。

添加选择。

$form->select(2)->set([
    "name"=>"my_select",
    "label"=>"My Select",
    "value" => "option3",
    "options" => [
        "First Group Test"=>"--group--",
        "Option 1" => [
            "value" => "option1",
            "custom-option-attribute" => "attribute value"
        ],
        "Option 2" => "option2",
        "Second Group Test"=>"--group--",
        "Option 3" => "option3",
        "Option 4" => "option4"
    ]
]);

单选按钮也需要一个选项数组。

$form->input(1)->set([
    "name"=>"my_radio",
    "type" => "radio",
    "value" => "value2",
    "label" => "This is a radio button",
    "options" => [
        "option 1" => "value1",
        "option 2" => "value2"
    ]
]);

value 属性将预选中选项数组中提供的选项。

添加 HTML 或空白。

$form->html(
    6, //grid width
    '<b>My Content</b>'
);

添加 Google ReCaptcha。您需要在 https://www.google.com/recaptcha/ 创建一个 API 密钥

$form->recaptcha("your api public key");

注意:您必须在您的页面上包含以下 JavaScript。 <script src='https://www.google.com/recaptcha/api.js'></script>

添加按钮。

$form->button(1)->set([
    "text"=>"My Button",
    "class"=>"btn-lg btn-primary"
]);

现在只需输出表单。

echo $form->compile()

捕获并处理已提交的数据

初始化类对象

use Teknicode\Form\Process;
$process = new Process("mail" or "sms"); //mail is default

使用如下所示的 set 方法提供设置

$process->set("recipient","email@address.co.uk" /*string or array of strings*/ );
$process->set("from",["address"=>"sender@address.co.uk","name"=>"Sender Name"]);

设置 SMTP 凭据 - 跳过此步骤以使用 mail() 发送

$process->set("smtp",[
  "Host"=>"mailerserver.url.com",
  "Username"=>"email@username",
  "Password"=>"AccountPassword",
  "SSL"=>"tls",
  "Port"=>587
]);

如果您使用 SMS,必须提供以下 AWS 凭据

$process->set("aws", [
   "aws_access_key_id" => "",
   "aws_secret_access_key" => "",
   "default_region" => "eu-west-1",
   "sms_sender_id" => ""
]);

//you would then provide a mobile number in recipient including the country code:
$process->set("recipient","+4407123456789");

如果您使用 MySQLi,必须提供以下凭据

$process->set("mysqli", [
   "host" => "",
   "username" => "",
   "password" => "",
   "database" => "",
   "table" => ""
]);

//you would then provide the values to be set:
$process->set("values",[
  "field name" => "value"
]]);

//to update an existing entry provide the id
$process->set("id",INT);

检查 Google ReCaptcha(可选)

$process->recaptcha("your api private key");
//used to check the recaptcha response

现在只需要捕获 post!

$send = $process->catch();

if( $send['status']=="failed" ){
  //do something with the error message
  echo $send['error'];

  //in the event of an error the post values are passed back //to repopulate the form
  $send['data'] contains $_POST
}

捕获方法将解析已提交的数据并创建一个包含输入名称和设置的简单干净的电子邮件。

可用设置的列表

电子邮件

$process->set("recipient",STRING | ARRAY OF STRINGS);

$process->set("from",["address"=>STRING,"name"=>STRING]);

$process->set("replyto",STRING);

$process->set("subject",STRING);

process->set("smtp",[
  "Host"=>STRING,
  "Username"=>STRING,
  "Password"=>STRING,
  "SSL"=>STRING,
  "Port"=>INT
]);

短信

$process->set("recipient","+4407123456789");

$process->set("aws", [
   "aws_access_key_id" => "",
   "aws_secret_access_key" => "",
   "default_region" => "eu-west-1",
   "sms_sender_id" => ""
]);

MySQLi

$process->set("id",INT); //optional for update instead of insert

$process->set("mysqli", [
   "host" => "",
   "username" => "",
   "password" => "",
   "database" => "",
   "table" => ""
]);

$process->set("values",[
  "field name" => "value"
]);

许可

版权 2018 Teknicode

特此授予任何获得此软件及其相关文档文件(以下简称“软件”)副本的任何人,免费使用该软件的权利,不受限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,以及准许向软件提供者提供软件的人使用该软件的权利,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他原因,无论该索赔、损害或其他责任是否与软件或其使用或其他交易有关。