【问题标题】:save value when going from one html to another从一个 html 转到另一个 html 时保存价值
【发布时间】:2016-04-05 15:39:00
【问题描述】:

我在 html 上的文本框上有一个值:

<input type="text" id="Key" name="Key" required />

然后我将操作发送到 php,然后我从 php 执行此操作:

<?php
header("location:./setupDatos.html");//echo "2"; //first time activation
echo '<input type="text" value="' . $varKey. '" />';

问题是,如果我删除这一行

header("location:./setupDatos.html");//echo "2"; //first time activation

它向我显示了一个空白页面,其中包含我发送到 php 的密钥(如预期的那样) 但是使用该行,它会更改为新的 html,并且带有发送键的输入无处可寻。如何将该值从第一个 html 发送到下一个 html?

【问题讨论】:

  • HTTP 协议是无状态的,因此要“记住”之前的状态,您要么必须通过 header("Location:./setupDatos.html?oldVariable=$value"); 传递它,要么将其保存在会话中。
  • @apokryfos 如何从 html 中获取该值?
  • @apokryfos 我的连接是通过 POST 而不是 GET 顺便说一句

标签: javascript php html


【解决方案1】:

HTTP 协议是无状态的,这意味着它无法“记住”较旧的帖子数据。但是,您可以使用会话来模拟状态。 尝试执行以下操作:

<?php
session_start();
$_SESSION["oldpostdata"] = $_POST;    
header("location:./setupDatos.html"); 

不知道这里有什么意义。 html 页面通常无法进行任何服务器端处理。但是,如果它能够访问您的旧帖子数据,例如:

setupDatos."html"

<?php    
session_start();
$oldPostData = $_SESSION["oldPostData"];
unset($_SESSION["oldPostData"]); // To only "flash" the data and not have it persist
$id = $oldPostData["Key"]; // Probably

“转发”取自this question 的帖子数据的另一种方式:

<!DOCTYPE html>
<html>
    <body onload="document.forms[0].submit()">
        <form action="new-location.php" method="post">
            <?php foreach( $_POST as $key => $val ): ?>
                <input type="hidden" name="<?= htmlspecialchars($key, ENT_COMPAT, 'UTF-8') ?>" value="<?= htmlspecialchars($val, ENT_COMPAT, 'UTF-8') ?>">
            <?php endforeach; ?>
        </form>
    </body>
</html>

【讨论】:

  • 我宁愿使用隐藏值。如何将 post 值从 php 发送到我的 html,然后像这样读取它: ?
  • 添加了一个替代方案,来自 stackoverflow.com/questions/6180072/php-forward-data-post 。不过,您确实应该更清楚您的问题需要什么。
  • 总体问题是没有直接的方式来转发 POST 参数,就像使用 GET 参数一样。它要么进行客户端重定向,要么使用 cURL(在链接的问题中也进行了讨论)。
猜你喜欢
  • 1970-01-01
  • 2015-05-16
  • 2014-07-02
  • 1970-01-01
  • 2016-12-30
  • 2019-07-15
  • 1970-01-01
  • 2022-11-29
  • 1970-01-01
相关资源
最近更新 更多