如何在 php 可重用表单中发送多复选框输入值?

How to send multi checkbox input value in php reusable forms?

我正在使用“reusableforms.com”PHP 表单脚本。该表格运行良好。但是我在多输入复选框值上出错。

如何获取多输入复选框的值?

HTML代码:

<div class="form-box">
    <label>Choose which of the following are most important to you:</label>
    <div class="multi-select">
        <div class="custom-select">
            <input type="checkbox" id="hight-rating" value="High Rating" name="important" hidden>
            <label for="hight-rating">High Rating</label>
        </div>
        <div class="custom-select">
            <input type="checkbox" id="mobile-phone-app" value="Mobile phone app" name="important" hidden>
            <label for="mobile-phone-app">Mobile phone app</label>
        </div>
        <div class="custom-select">
            <input type="checkbox" id="vendor-management" value="Vendor management" name="important" hidden>
            <label for="vendor-management">Vendor management</label>
        </div>
    </div>
</div> 

handler.php

<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    require_once './vendor/autoload.php';
    
    use FormGuide\Handlx\FormHandler;    
    
    $pp = new FormHandler(); 
    
    $validator = $pp->getValidator();
    $validator->fields(['name','email', 'phone', 'address', 'property', 'following', 'units', 'date'])->areRequired()->maxLength(50);
    $validator->field('important[]')->maxLength(5000);
    $validator->field('email')->isEmail();
    
    
    $pp->sendEmailTo('xxx@gmail.com'); // ← Your email here
    
    echo $pp->process($_POST);
?>

谢谢。

根据你的问题,你没有将复选框声明为你需要的数组:

<input type="checkbox" id="hight-rating" value="High Rating" name="important[]" hidden>

但如果这没有帮助,那么再详细说明一下这个问题会很有用。

用于html

    <input type="checkbox" id="mobile-phone-app" value="Mobile phone app" name="important[]" hidden>

在 php 中你会收到数组中的数据所以你大喊使用计数而不是 maxLength

$important = $_GET['important'];
$count = count($important);

我们可以通过jQuery附加函数获取表单数据。

$("#reused_form").submit(function (e) {
    e.preventDefault();
    $form = $(this);

    var formdata = new FormData(this);
    
    var valuesArray = $('[name=important]:checked').map( function() {
        return this.value;
    }).get().join(", ");

    formdata.append( 'important', valuesArray );

    $.ajax({
        type: "POST",
        url: "form/handler.php",
        data: formdata,
        success: after_form_submitted,
        dataType: "json",
        processData: false,
        contentType: false,
        cache: false,
    });
});