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

如何在PHP页面间传递表单数据?求技术解决方案

解决PHP页面间表单数据传递的问题

先看你当前代码里的几个关键问题,这些会直接导致数据传不过去:

  • 「Save」按钮在<form>标签外面,点击它不会触发表单提交
  • 多个输入框(SKU、Name、Price、Size等)没设置name属性,或者重复用了相同的name(比如SKU和Name都用了fname),PHP无法识别这些字段
  • 产品类型的下拉框也没有name属性,提交后拿不到选择的产品类型

第一步:修复表单页面(第一页)的代码

把提交按钮移到表单内部,给所有需要提交的输入框加上唯一的name属性:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="add.css">
</head>
<body>
    <div class="flex_container">
        <div class="product_add"><b>Product Add</b></div>
    </div>

    <hr size="2" color="rgb(156,1,0)" width="95%" style="clear: both;">
    <br>
    <div>
        <form action="main.php" method="post" id="product_form">
            <!-- 给SKU设置唯一name -->
            <div>
                <label for="sku">SKU</label>
                <input type="text" id="sku" name="sku">
            </div>
            <!-- 给Name设置唯一name -->
            <div>
                <label for="name">Name</label>
                <input type="text" id="name" name="product_name">
            </div>
            <!-- 给Price加name -->
            <div>
                <label for="number">Price ($)</label>
                <input type="number" id="price" name="price" min="1" step="any" />
            </div>
            <!-- 给下拉框加name -->
            <div>
                <label for="type">Type Switcher</label>
                <select id="productType" name="product_type" onclick="display();">
                    <option value="null" selected disabled>Choose one...</option>
                    <option value="disc" id="DVD">DVD-disc</option>
                    <option value="book" id="Book">Book</option>
                    <option value="furniture" id="Furniture">Furniture</option>
                </select>
                <!-- 给Size加name -->
                <div id="a" style="display: none;">
                    <label for="size">Size (MB)</label>
                    <input id="size" name="size" type="number" />
                    <h4>Please provide Size in MB !</h4>
                </div>
                <!-- 给Weight加name -->
                <div id="b" style="display: none;">
                    <label for="weight">Weight (KG)</label>
                    <input id="weight" name="weight" type="number" />
                    <h4>Please provide Weight in KG !</h4>
                </div>
                <!-- 给Height/Width/Length加name -->
                <div id="c" style="display: none;">
                    <div>
                        <label for="height">Height (CM)</label>
                        <input id="height" name="height" type="number" />
                    </div>
                    <div>
                        <label for="width">Width (CM)</label>
                        <input id="width" name="width" type="number" />
                    </div>
                    <div>
                        <label for="length">Length (CM)</label>
                        <input id="length" name="length" type="number" />
                    </div>
                    <h4>Please provide Height, Width, and Length in CM !</h4>
                </div>
                <!-- 把Save按钮移到表单内部 -->
                <button type="submit" name="submit" id="save_btn">Save</button>
            </div>
        </form>
    </div>
    <script src="add.js"></script>
</body>
</html>

第二步:在接收页面(main.php)获取并处理数据

在main.php里,用PHP的$_POST超全局变量接收表单数据,这里做简单的显示示例,你也可以改成存入数据库等逻辑:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="main.css">
</head>
<body>
    <div class="flex_container">
        <div class="product_list"><b>Product List</b></div>
        <a href="add-product.html"><button id="add_button">ADD</button></a>
        <button id="delete_button">MASS DELETE</button>
    </div>
    <hr size="2" color="rgb(156,1,0)" width="95%" style="clear: both;">
    <div>
        <?php
        // 判断是否有表单提交
        if(isset($_POST['submit'])){
            // 获取字段数据,用htmlspecialchars过滤防止XSS攻击
            $sku = htmlspecialchars($_POST['sku']);
            $productName = htmlspecialchars($_POST['product_name']);
            $price = htmlspecialchars($_POST['price']);
            $productType = htmlspecialchars($_POST['product_type']);
            
            // 根据产品类型获取对应属性
            $additionalInfo = '';
            if($productType === 'disc'){
                $additionalInfo = 'Size: ' . htmlspecialchars($_POST['size']) . ' MB';
            } elseif($productType === 'book'){
                $additionalInfo = 'Weight: ' . htmlspecialchars($_POST['weight']) . ' KG';
            } elseif($productType === 'furniture'){
                $height = htmlspecialchars($_POST['height']);
                $width = htmlspecialchars($_POST['width']);
                $length = htmlspecialchars($_POST['length']);
                $additionalInfo = "Dimensions: {$height}x{$width}x{$length} CM";
            }
            
            // 显示提交的数据
            echo "<h3>提交的产品信息:</h3>";
            echo "<p>SKU: {$sku}</p>";
            echo "<p>产品名称: {$productName}</p>";
            echo "<p>价格: \${$price}</p>";
            echo "<p>产品类型: {$productType}</p>";
            echo "<p>{$additionalInfo}</p>";
        }
        ?>
    </div>
    <hr size="2" color="rgb(156,1,0)" width="95%" style="clear: both;">
</body>
</html>

额外说明:PHP页面间传数据的常用方式

  • POST方式:就是你现在用的,适合传递表单数据、敏感数据(数据不会显示在URL里)
  • GET方式:把数据放在URL里传递,比如main.php?sku=123&name=test,适合非敏感的、需要分享的链接,但数据长度有限制
  • SESSION会话:如果需要在多个页面间持续保留数据(比如用户登录状态),可以用session_start()开启会话,把数据存在$_SESSION数组里,比如$_SESSION['sku'] = $sku,之后其他页面只要开启会话就能访问

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 15:37:09