如何从两个不同的 php 页面获取变量一起显示在第三个 php 页面?

How can you get variables taken from two different php pages to show up together at at a third php page?

提问人:schoolwithschool 提问时间:6/6/2018 最后编辑:Pradeepschoolwithschool 更新时间:6/7/2018 访问量:24

问:

<!--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 变量 undefined-index

评论

0赞 NitinSingh 6/6/2018
请将每段代码分开

答:

0赞 Sambhaji Katrajkar 6/6/2018 #1

你的 p3.php 应该是:

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

您已经投入了 _POST 美元,这应该只是$formform

0赞 Aljay 6/6/2018 #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>
    ";
?>

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

在 p3 上这样做。

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

从 p2.php 中获取名为“form”的隐藏输入的值

0赞 Mr Glass 6/6/2018 #3

在 p2.php 前面添加以下行</form>

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

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