【问题标题】:Reading Data from two PHP pages into the third Page从两个 PHP 页面读取数据到第三个页面
【发布时间】:2017-11-17 21:09:33
【问题描述】:

我无法合并两个页面的数据。我只能显示一页的数据。我试图在谷歌上搜索我的问题的解释,但我找不到它。 如您所见,我收到“未定义索引”错误 [![in this screenshot][1]][1]。 不能在这里只放代码,因为它一直告诉我我需要在所有这些事情中使用 ctrl + K 和我的新手所以请原谅我..

<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
</head>

<body>
 <form action = "page2.php" method = "POST">
                            <select list="Country" placeholder="Country" name="country" required class="form-control" style="max-width:250px; margin-top:50px;" id="Country">
                                <option >Bosnia & Herzegovina</option>
                                <option >Croatia</option>
                                <option >Serbia</option>
                                <option >England</option>
                                <option >Germany</option>
                                <option >Austria</option>
                                <option >Belgium</option>
                                <option >Switzerland</option>
                                <option >Italy</option>
                                <option >Romania</option>
                                <option >France</option>
                                <option >Montenegro</option>
                                <option >Slovenia</option>
                            </select>

            <input type="email" name="email" id="email" placeholder="Your email address" required class="form-control" style="max-width:250px;" />

            <input type="password" name="password" id="creapass" placeholder="Create your password" required  class="form-control" style="max-width:250px;" />

            <input type="password" name="password2" id="password" placeholder="Confirm your password" required  class="form-control" style="max-width:250px;" />

            <a href="page2.php"> <input type="button" value="Next" class="btn btn-primary" style="width:150px;"></a>

            </form>

</body>
</html>

第二页

<!DOCTYPE html>
<html>
<head>
<title>test2</title>

</head>

<body>
<form action = "finalpage.php" method = "POST">

    <!-- Values from First Step -->

<input type="hidden" name="country" value="<?php $_POST['country'] ?>">

<input type="hidden" name="email" value="<?php $_POST['email'] ?>">

<input type="hidden" name="password" value="<?php $_POST['password'] ?>">
<input type="hidden" name="password" value="<?php $_POST['password2'] ?>">

<!-- End of Values from First Step -->

<?php
echo "Country:" .$_POST["country"]."</br>";
echo "Email:".$_POST["email"]."</br>";
echo "Password:".$_POST["password"]."</br>";
echo "Password2:".$_POST["password2"]."</br>";
?>


                                <input type="text" name="first" class="form-control" placeholder="First name" required style="max-width:250px;" >

                                <input type="text" name="last" class="form-control" placeholder="Last name" required style="max-width:250px;" />

                                <input type="date" name="date" class="dateb" id="dateOfBirth" required  />

                                        <select list="Country" placeholder="Country" name="country2" required class="form-control" style="max-width:250px; " id="Country">
                                            <option >Bosnian</option>
                                            <option >Croat</option>
                                            <option >Serb</option>
                                            <option >English</option>
                                            <option >German</option>
                                            <option >Austrian</option>
                                            <option >Belgian</option>
                                            <option >Swiss</option>
                                            <option >Italian</option>
                                            <option >Romanian</option>
                                            <option >French</option>
                                            <option >Montenegrin</option>
                                            <option >Slovenian</option>
                                        </select>

                                <input type="text" class="form-control" name="street1" placeholder="Street address 1" required style="max-width:250px;" />

                                <input type="text" class="form-control" name="street2" placeholder="Street address 2 (Optional)" style="max-width:250px;" />

                                <input type="text" class="form-control" name="city" placeholder="City" required style="max-width:250px;" />

                                <input type="text" class="form-control" name="region" placeholder="Province/Region" required style="max-width:250px;" />

                                <input type="text" class="form-control" name="postal" placeholder="Postal code" required style="max-width:250px;" />

                                <input type="tel" class="form-control" name="phone" placeholder="Phone number" required style="max-width:250px;" />

                                <input type="checkbox" name="terms" > I have read and agree to the Terms and WBC's User Agreement.

                                <a href="LinkCard.html"><input type="Submit" name="insert" value="Continue" class="btn btn-primary btn-lg"></a>


            </form>

</body>

</html>

最后一步。

<html>
<head>
<title>get data from another page</title>
</head>
<body>
<?php
echo "Country:" .$_POST["country"]."</br>";
echo "Email:".$_POST["email"]."</br>";
echo "Password:".$_POST["password"]."</br>";
echo "Password2:".$_POST["password2"]."</br>";

echo "First name:" .$_POST["first"]."</br>";
echo "Last name:".$_POST["last"]."</br>";
echo "Date:".$_POST["date"]."</br>";
echo "Country2:".$_POST["country2"]."</br>";
echo "Street1:" .$_POST["street1"]."</br>";
echo "Street2:".$_POST["street2"]."</br>";
echo "City:".$_POST["city"]."</br>";
echo "Region:".$_POST["region"]."</br>";
echo "Postal code:" .$_POST["postal"]."</br>";
echo "Phone number:".$_POST["phone"]."</br>";
?>
</body>
</html>

【问题讨论】:

  • 如果我理解正确,您希望有一个两步的表单提交过程。每一步都是不同的页面。对吗?
  • 是的,没错。
  • @GrumpyCrouton,您的反对票不仅有效,而且值得赞赏,因为它很有价值,而不仅仅是简单的反对票。
  • @GrumpyCrouton,感谢您分享此信息。这将帮助每个人提高他们的问题/答案的质量。

标签: php indexing undefined


【解决方案1】:

我不会复制您的整个代码,因为它不是必需的。以下是您必须遵循的逻辑。

在第一步中,即您的第一个表单(我更喜欢使用 PHP 格式,因为它允许您在需要时使用 PHP 函数),将您的第二个 PHP 放入表单的操作中。

<form action = "step_2.php" method = "POST">

现在在您的step_2.php 中,您必须在表单中添加以下内容。

<!-- Values from First Step -->

<input type="country" name="country" value="<?php echo $_POST['country'] ?>">

<input type="hidden" name="email" value="<?php echo $_POST['email'] ?>">

<input type="hidden" name="password" value="<?php echo $_POST['password'] ?>">

<!-- End of Values from First Step -->

以上代码会将第一步发布的值添加到您的第二步表单中。现在您可以在最后一步中简单地获取这些值。为此,请在此表单的 Action 中添加最后一步 PHP。

<form action = "final_step.php" method = "POST">

在您的final_step.php 中,您通常可以通过$_POST 获取值。

【讨论】:

  • 我又遇到了同样的问题,它没有保存第一步注册的数据。
  • 尝试echo每一步的所有值。确保您的所有文件都是.php 而不是.html
  • 所有值都是 echo 并且所有内容都在 .php 中,但第一个注册页面总是让我遇到问题...prnt.sc/hbvdi5
  • 编辑您的问题并将您的代码粘贴为代码而不是代码片段。对所有三个文件执行此操作。
  • 顺便说一下,尝试从所有表单中的&lt;input type="submit"&gt; 中删除&lt;a href=""&gt;
【解决方案2】:

你有很多问题。首先,您的选项字段没有值。

你有什么:&lt;option&gt;Bosnia&lt;/option&gt;

你需要什么:&lt;option value="bosnia" &gt;Bosnia&lt;/option&gt;


您还应该将默认值作为您的第一个选项,否则您的程序将假定每个不选择某项内容的人都有第一个选项,您将收到一百万个不准确的提交。例如:

<option value="false" selected="selected">Please select an option from below</option>

那么你的后端应该像这样检查:

<?php
if (!isset($_POST['country'] || $_POST['country'] === 'false')
{
    //They did not pick a country, and you need to bounce them back to the form with an error message.
    header('Location: /path/to/your/form');
    exit;
}
//country is defined, you can continue,
//but you should pre-validate the other required field here also.
//do not assume things are right, assumptions = bugs.
...

您的提交按钮周围的锚标记可能正在重定向而不是提交表单,在这种情况下,您的$_POST 将完全为空。您拥有以下标签:

<a href="page2.php"> <input type="button" value="Next" class="btn btn-primary" style="width:150px;"></a>

需要看起来更像这样:

<input type="submit" value="Next" class="btn btn-primary" style="width:150px;">

或者:

<button type="submit" class="btn btn-primary" style="width:150px;">Next</button>

如果你被难住了,把它放在你处理代码的开头:

echo '<pre>' . print_r($_POST, 1); exit;

这将向您显示提交的内容,并让您根据需要从那里完成工作。


一般来说,你的条件句应该在验证它之前检查一个键是否存在,如果它是否被设置有任何歧义:

//BAD
if ($_POST['country'] == 'someval') { ... }

//GOOD
if (array_key_exists('country', $_POST) && $_POST['country'] == 'someval') { ... }

您也可以使用isset($_POST['country']); 进行检查但是,如果null 值对您正在检查的任何内容都有效,那么即使密钥在那里,这也将返回false。因此,在null 有效的任何情况下,您都需要使用array_key_exists()


与其将变量直接从一个页面传递到另一个页面,不如使用$_SESSION 会容易得多,因此,如果它们反击或走开一会儿,值并不会完全丢失。用户讨厌不得不重复做一些事情,如果他们不得不这样做,他们可能会完全失去对您网站的兴趣。相反,如果它们存在于会话内存中,您可以像这样设置值:

<?php
session_start(); //you have to do this before printing ANY other content.
$_SESSION['country'] = (isset($_POST['country']) ? $_POST['country'] : null);

然后您可以随时从任何页面检索此内容:

<?php
session_start();
echo $_SESSION['country'];

如果你想清除它们,你只需这样做:

<?php
session_start(); //can't destroy a non-existent session, you have to do this first
session_destroy(); //clears the session
session_start(); //starts a new one

在会话中暂时存储正在进行的表单值可以让您在 会话 cookie 持续的时间内访问它们,因此如果用户退出您的表单,转到另一个站点,然后它们就不会丢失回来完成。不过,您的表单必须手动将它们放回原处,因此输入可能看起来像这样

<input type="password" name="password" value="<?= isset($_SESSION) && isset($_SESSION['password'] ? $_SESSION['password'] : null; ?>" />

我试图解释你没有应用的相关逻辑,而不仅仅是具体的用例,因为如果没有真正理解潜在的逻辑缺失是什么,否则你可能会再次碰壁。希望这可以帮助您避免一些其他错误,否则您可能会在为您修复此特定问题后立即遇到这些错误。

【讨论】:

    猜你喜欢
    • 2013-09-29
    • 2011-01-26
    • 1970-01-01
    • 2016-01-28
    • 2016-08-12
    • 2011-04-26
    • 1970-01-01
    • 2018-11-15
    • 2013-06-18
    相关资源
    最近更新 更多