You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将主题文件中的textarea字段改为多选checkbox选项

Hey there! Let's walk through how to convert that textarea into a set of multi-select checkboxes. Here's a step-by-step breakdown tailored to your existing code:

Convert Textarea to Multi-Select Checkboxes

First, let's define the options you want for the checkboxes (adjust these to match your actual tour description categories):

<?php
// Define checkbox options (value => human-readable label)
$tour_desc_options = [
    "family_friendly" => __("Family Friendly", ST_TEXTDOMAIN),
    "adventure_focused" => __("Adventure Focused", ST_TEXTDOMAIN),
    "cultural_experience" => __("Cultural Experience", ST_TEXTDOMAIN),
    "relaxation_oriented" => __("Relaxation Oriented", ST_TEXTDOMAIN)
];

// Handle selected values: convert saved data to array if needed
$selected_descs = STInput::request("st_desc", []);
// If your saved $excerpt is a comma-separated string, convert it to an array for pre-checking
if (!empty($excerpt) && is_string($excerpt)) {
    $selected_descs = explode(",", $excerpt);
}
?>

Next, replace your entire textarea block with this checkbox group code:

<div class="form-group">
    <label class="head_bol"><?php _e("Tour description", ST_TEXTDOMAIN) ?> <span class="text-small text-danger">*</span>:</label>
    <div class="checkbox-group">
        <?php foreach ($tour_desc_options as $value => $label): ?>
            <div class="form-check">
                <input 
                    class="form-check-input" 
                    type="checkbox" 
                    name="st_desc[]" 
                    id="desc_<?php echo esc_attr($value) ?>" 
                    value="<?php echo esc_attr($value) ?>"
                    <?php echo in_array($value, $selected_descs) ? "checked" : "" ?>
                >
                <label class="form-check-label" for="desc_<?php echo esc_attr($value) ?>">
                    <?php echo esc_html($label) ?>
                </label>
            </div>
        <?php endforeach; ?>
    </div>
    <div class="st_msg">
        <!-- Keep your existing message content here -->
    </div>
</div>

Key Things to Update:

  • Backend Data Handling: Since checkboxes submit as an array (st_desc[]), you need to adjust the code that saves this data. Convert the array to a comma-separated string (or serialize it) for storage:
    // When saving the form data
    $selected_descs = isset($_POST['st_desc']) ? $_POST['st_desc'] : [];
    $saved_value = implode(",", $selected_descs); // Save this string to your database
    
  • Required Field Validation: The original field is marked as required, so add frontend JS validation to ensure at least one checkbox is selected, or add backend checks to reject empty st_desc arrays.
  • Styling: Add CSS to .checkbox-group and .form-check to make the checkboxes display neatly (stacked or inline, depending on your theme's design).

That’s all! You’ll now have a functional multi-select checkbox group that pre-loads saved values and works with your existing form structure.

内容的提问来源于stack exchange,提问作者Amine Bedda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:27:48