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

PHP表单提交后无法隐藏表单并显示确认信息的问题

表单提交后未切换到确认界面的问题解决

问题根源

index.php中通过isset($_POST['name_entered'])判断是否显示确认信息,但表单提交后会跳转到process.php处理,随后重定向回index.php时,POST数据会丢失,导致index.php无法检测到$_POST['name_entered'],因此始终显示初始表单。

解决方案

改用Session存储提交状态,替代对POST数据的依赖:

  • 在process.php中,当用户提交姓名表单时,将提交状态存入Session
  • index.php中通过Session判断是否已提交表单,从而切换显示内容
  • 处理"Reset Count"和"Claim Again"按钮时,同步更新Session状态

修改后的process.php代码

<?php 
session_start();

// 初始化计数器
if(!isset($_SESSION['counter'])){
    $_SESSION['counter'] = 10;
}

// 处理重置计数器
if(isset($_POST['reset_btn'])){
    $_SESSION['counter'] = 10;
    // 重置提交状态,让用户可以重新提交表单
    unset($_SESSION['submitted']);
    unset($_SESSION['name_entered']);
}
// 处理姓名提交
elseif(isset($_POST['name_entered']) && !empty($_POST['name_entered'])){
    $_SESSION['name_entered'] = $_POST['name_entered'];
    $_SESSION['submitted'] = true;
    $_SESSION['counter'] -= 1;
}
// 处理再次领取
elseif(isset($_POST['claim_again_btn'])){
    $_SESSION['counter'] -= 1;
    // 重置提交状态,允许重新输入姓名
    unset($_SESSION['submitted']);
    unset($_SESSION['name_entered']);
}

header("Location: index.php");
?>

修改后的index.php代码

<?php session_start();?>
<html>
    <body>
        <main>
        <?php 
                // 通过Session判断是否已提交表单
                if(isset($_SESSION['submitted'])){
                    if($_SESSION['counter'] <= 10 && $_SESSION['counter'] > 0) {
                        ?>
                        <h2>Welcome <?php echo $_SESSION['name_entered']; ?>!</h2>
                        <p>We are giving away <span class="bold_text">free coupons</span> as token of appreciation</p>
                        <section>
                            <h4>50% discount</h4>
                            <h1 id="random_num"><?= rand(1000000,9999999);?></h1>
                            <form action="process.php" method="post">
                                <input type="submit" class="btn1" name="reset_btn" value="Reset Count">
                                <input type="submit" class="btn2" name="claim_again_btn" value="Claim Again">
                            </form>
                        </section>
                    <?php } else { ?>
                        <h2>Welcome <?php echo $_SESSION['name_entered']; ?>!</h2>
                        <p>We are giving away <span class="bold_text">free coupons</span> as token of appreciation</p>
                        <section>
                            <h3>Sorry!</h3>
                            <h1>Unavailable!</h1>
                            <form action="process.php" method="post">
                                <input type="submit" class="btn1" value="Reset Count" name="reset_btn">
                            </form>
                        </section>
    <?php 
                    }
                } else { 
                    // 初始化计数器(防止未访问process.php时counter不存在)
                    if(!isset($_SESSION['counter'])){
                        $_SESSION['counter'] = 10;
                    }
                    ?>
                    <h2>Welcome Customer!</h2>
                    <div class="first_message">
                    <p>We are giving away <span class="bold_text">free coupons</span> as token of appreciation for first <?= $_SESSION['counter'] ?> customer(s)</p>
                    <form action="process.php" method="post">
                        <label for="name">Kindly type your name: </label>
                        <input type="text" id="name" name="name_entered" required>
                        <input type="submit" id="submit" value="Submit" name="submit">
                    </form>
                </div>
                <?php }
                ?>   
        </main>
    </body>
</html>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 13:40:09