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

WordPress插件双表单问题:第二个表单提交按钮无echo输出

问题分析与解决方案

问题根源

你的代码逻辑存在一个核心问题:第二个表单的echo输出判断被嵌套在hotel_booking_process_form函数中,而这个函数仅在第一个表单的submit参数存在时才会执行。当用户提交第二个表单时,$_POST中只有confirm参数,没有submit,因此hotel_booking_process_form不会被调用,echo语句自然无法执行。

修复步骤

1. 分离表单处理逻辑

将第二个表单的提交处理移到外层,确保只要检测到confirm参数就触发输出;同时用Session保存第一个表单的提交状态,避免页面刷新后丢失第二个表单。

2. 修改核心代码

首先在插件初始化时启动Session(用于保存状态):

add_action('init', function() {
    if (!session_id()) {
        session_start();
    }
});

修改主表单函数hotel_booking_form:

function hotel_booking_form()
{
    // 优先处理第二个表单的提交
    if (isset($_POST['confirm'])) {
        echo '*****************';
        // 这里可以添加更多后续处理逻辑,比如保存数据
    }

    $arrival_date = isset($_POST['arrival-date']) ? $_POST['arrival-date'] : (isset($_SESSION['arrival-date']) ? $_SESSION['arrival-date'] : '');
    $departure_date = isset($_POST['departure-date']) ? $_POST['departure-date'] : (isset($_SESSION['departure-date']) ? $_SESSION['departure-date'] : '');
    $adults = isset($_POST['adults0']) ? $_POST['adults0'] : '0';
    $children = isset($_POST['children0']) ? $_POST['children0'] : '0';
    $children_ages = isset($_POST['child-age-0']) ? $_POST['child-age-0'] : array();

    // 标记第一个表单是否已提交
    $first_form_submitted = isset($_POST['submit']) || isset($_SESSION['first_form_submitted']);

    // 提交第一个表单时,保存数据到Session
    if (isset($_POST['submit'])) {
        $_SESSION['arrival-date'] = $arrival_date;
        $_SESSION['departure-date'] = $departure_date;
        $_SESSION['first_form_submitted'] = true;
    }

    ob_start();

    if (!$first_form_submitted) {
        // 显示第一个表单
        ?>
        <form id="hotel-booking-form" method="post" action="">
            <label for="datetimes">Arrival and Departure Dates:</label>
            <input type="text" name="datetimes" id="datetimes" value="<?php echo ($arrival_date && $departure_date) ? ($arrival_date . ' - ' . $departure_date) : ''; ?>" />
            <input type="hidden" name="arrival-date" id="arrival-date" value="<?php echo $arrival_date; ?>" />
            <input type="hidden" name="departure-date" id="departure-date" value="<?php echo $departure_date; ?>" />

            <?php
            render_chambre_form(0, $adults, $children, $children_ages);

            $chambreIndex = 1;
            while (isset($_POST['adults' . $chambreIndex])) {
                $adults = $_POST['adults' . $chambreIndex];
                $children = $_POST['children' . $chambreIndex];
                $children_ages = isset($_POST['child-age-' . $chambreIndex]) ? $_POST['child-age-' . $chambreIndex] : array();
                render_chambre_form($chambreIndex, $adults, $children, $children_ages);
                $chambreIndex++;
            }
            ?>

            <div id="additional-chambres-container">
            </div>

            <input type="button" id="add-chambre-btn" value="Add room">
            <input id="btn-submit" type="submit" name="submit" value="Submit">
        </form>
        <script src="<?php echo plugins_url('assets/js/hotel-booking.js', __FILE__); ?>"></script>
        <?php
    } else {
        // 显示第二个表单
        hotel_booking_process_form();
    }

    return ob_get_clean();
}

修改hotel_booking_process_form函数,移除内部的confirm判断:

function hotel_booking_process_form()
{
    // 从Session获取第一个表单的数据(按需使用)
    $arrival_date = $_SESSION['arrival-date'];
    $departure_date = $_SESSION['departure-date'];
    ?>
    <div id="additional-form-container">
        <form id="additional-form" method="post" action="">
            <label for="first_name">First Name:</label>
            <input type="text" name="first_name" id="first_name" required><br>
            <label for="last_name">Last Name:</label>
            <input type="text" name="last_name" id="last_name" required><br>
            <label for="phone">Phone:</label>
            <input type="tel" name="phone" id="phone" required><br>
            <button type="submit" name="confirm">Confirm</button>
        </form>
    </div>
    <?php
}

额外注意事项

  • 处理完第二个表单后,可根据需求清除Session中的状态标记,让用户能重新发起预订流程。
  • 务必对所有用户输入进行验证和 sanitize,避免SQL注入、XSS等安全风险。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 23:15:13