【问题标题】:How can you get variables taken from two different php pages to show up together at at a third php page?如何让来自两个不同 php 页面的变量一起显示在第三个 php 页面上?
【发布时间】:2018-11-15 16:42:37
【问题描述】:
<!--first page [p1.php]-->
            <!DOCTYPE html>
            <html>
                <head>
                </head>
                <body>
                    <form method = 'post' action = 'p2.php'>
                        Form <input type = 'text' name = 'form' /><br><br>
                        <input type = 'submit' value = 'Next' />
                    </form>
                </body>
            </html>
<!--second page [p2.php]-->
            <?php               
            //Log inputs
                $form = $_POST['form'];
            //Echo variables
                echo "
                        <form method = 'post' action= 'p3.php'> 
                            $form<br>
                            <b>Question 1: </b>Type websites's name<br>
                            <b>Website </b><input type = 'text' name = 'website' /><br><br>
                            <input type = 'submit' value = 'Submit' />
                        </form>
                    ";
            ?>
<!--page 3 [p3.php]--> 
            <?php           
            //Log inputs
                $form= $_POST['$form'];
                $website = $_POST['website'];
            //Echo variables
                echo "$form $website<br>";
            ?>
            On [p3.php] it gives me an error stating:

注意:未定义的索引:第 3 行 [p3.php 的路径] 中的表单 堆栈溢出

如何使 p3.php 显示 p2.php 中的 $form 和 $website?

【问题讨论】:

  • 请将每段代码分开

标签: php variables undefined-index


【解决方案1】:

在 p2.php 中 &lt;/form&gt; 之前添加以下行

<input type='hidden' name='form' value='$form'>

如果您想要更多这些,您可能希望将变量存储在 cookie 中(如果用户未启用 cookie,则作为 URL 参数)。

【讨论】:

    【解决方案2】:

    你在第 2 页和第 3 页做错了。

    在第 2 页:

    像这样更改您的代码。

    <?php
    //Log inputs
    $form = $_POST['form'];
    //Echo variables
    echo "
        <form method = 'post' action= 'p3.php'> 
            $form<br>
            <input type='hidden' value='$form' name='form'/>
            <b>Question 1: </b>Type websites's name<br>
            <b>Website </b><input type = 'text' name = 'website' /><br><br>
            <input type = 'submit' value = 'Submit' />
        </form>
        ";
    ?>
    

    使用 hidden 输入类型并将其命名为“form”,然后将表单中的值放在 p1.php 中。

    在 p3 上这样做。

    <?php           
        //Log inputs
        $form= $_POST['form'];
        $website = $_POST['website'];
        //Echo variables
        echo "$form $website<br>";
    ?>
    

    从 p2.php 中获取名为 'form' 的 input hidden 的值

    【讨论】:

      【解决方案3】:

      你的 p3.php 应该是:

      <?php           
         //Log inputs
         $form= $_POST['form'];
         $website = $_POST['website'];
         //Echo variables
         echo "$form $website<br>";
      ?>
      

      您在 $_POST 中给出的 $form 应该只是 form

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-30
        • 1970-01-01
        • 2014-10-24
        • 2012-04-08
        • 1970-01-01
        • 2014-03-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多