【问题标题】:Wordpress PHP: The variables are lost on the redirected pageWordpress PHP:变量在重定向页面上丢失
【发布时间】:2016-02-12 16:56:06
【问题描述】:

我在Wordpress中做了一个简单的表格如下:

<form action="../process.php" method="post" name="myForm">

vol_single <input id="vol_single" type="text" name="vol_single" />
rate_single <input id="rate_single" type="text" name="rate_single" />

<input type="submit" value="Submit" /></form>

这里是process.php:

<?php 
session_start();
  $vol_single = $_POST['vol_single'];

  $rate_single = $_POST['rate_single'];

  $rate_total=12*($vol_single*$rate_single);

//Redirects to the specified page
header("Location: http://..."); 
exit();
?>

我正在尝试在 Wordpress 的重定向页面上显示计算值。我试过这个:

[insert_php]
echo 'total rate:';
echo $rate_total;
[/insert_php]

它不显示任何值。有人可以帮忙吗?

【问题讨论】:

  • php 是无状态的 - 重定向会导致销毁所有变量的拆卸。如果要在请求之间持久化数据,则需要将其附加为查询字符串参数,或保存到持久存储(SESSION、数据库、文件等)
  • 感谢您的意见。

标签: php wordpress forms redirect


【解决方案1】:

重定向后您将无法访问变量。变量的生命周期在每个脚本执行后结束。

为此,您可以将变量存储在会话中,也可以将它们附加到重定向目标的末尾,然后从新页面上的 $_GET 超全局变量中访问它们,如下所示:

First Page:
header("Location: http://example.com?total=$rate_total");

Second Page:
echo 'Total is: ' . $_GET['total'];

您可以找到关于会话的快速介绍here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 2014-08-14
    • 1970-01-01
    • 2014-03-23
    • 2016-07-02
    • 1970-01-01
    相关资源
    最近更新 更多