首页 > 解决方案 > PHP头位置变量重定向和主页输出

问题描述

我有一个页面,form它发出一个 POST 请求,并且在我输入姓氏的地方有一个输入。

提交表单后,我想从SESSION['surname']提交的变量中获取,然后通过header("Location...").

我希望最后一个变量名称出现在表单下的主页上。怎么做?

有人可以指导吗?

标签: phpformspostheader

解决方案


类似的东西;带有表格的主页:

<form action="src/session.php" method="POST">
<div>SESSION</div>
<div>
  <label for="surname">Surname</label>
  <input name="surname">
</div>
<button type="submit" name="approve" value="approval">Send</button>
</form>

在 src/session.php 中:

<?php
if($_POST['approve']) {
    $_SESSION['surname'] = urlencode($_REQUEST['surname']);
    $var = $_SESSION['surname'];
}

header("Location: https://....");   #here should be the home page with the form and after redirect i want to see exactly same form but with $var underneath it
?>

推荐阅读