Php 新手,如何从新的 html 表单中获取 php 变量

Php newbie, how to get php variable from new html form

你好我正在尝试使用一种形式将 post 从 index.html 到 action_page.php 的变量,然后以某种方式从新的 results.html 页面获取这些变量(这可能吗?)

我的索引表格是这样的

<form action="action_page.php" method="post">
<select name="race" style="width: 180px;">
<option value="White">White</option>
<option value="Asian">Asian</option> // 
</select>

edit: 
<p><input type="submit" value="formSubmit" name="formSubmit"></p>

</form> </p> <!-- there is a lot more code in between -->

我的 php 看起来像这样

<?php
if(isset($_POST["formSubmit"]) )
{
  $varRace = $_POST["race"];

echo $varRace; // this doesn't work why?

}

function redirect($url, $statusCode = 303)
{
header('location: ' .$url, true, $statusCode);
die();
}
$varRedirect = "results.html";
// call to function removed but it would call redirect($varRedirect);
?>

最终我希望我的 results.html 页面显示一个变量 $varRace。

因为自己形成 post 这可能得到吗?有没有我可以写的 php 函数来发送变量?

你可以使用 cookie 从 action_page.php 获取发布的数据并将数据设置为名为 race end 的 cookie 值,然后在 results.html 中你可以获得该值并将其打印在 results.html 然后删除 cookie(设置过期时间)

#先关闭form标签并添加在你的表单中输入类型="submit"

<form action="action_page.php" method="post">
 <select name="race" style="width: 180px;">
 <option value="White">White</option>
 <option value="Asian">Asian</option> // 
 </select>
<input type="submit" name="formSubmit" value="submit">>
</form>

改变action_page.php

<?php
    if(isset($_POST["formSubmit"]) )
    {
    $varRace = $_POST["race"];
    setcookie("race", $varRace, time()+600);
    $varRedirect = "results.html";
    redirect($varRedirect);
    }

    function redirect($url, $statusCode = 303)
    {
        header('location: '.$url, true, $statusCode);
    }

 ?>

results.html

<html>
<head>
    <script type="text/javascript">
        function init() {
            var race = getCookie('race');
                document.getElementById("test").innerHTML = race;
                document.cookie = "race=; expires=Thu, 01 Jan 1970 00:00:00 UTC"; 
        }

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
} 
    </script>
</head>
<body onload="init()">
    your race is :
    <p id='test'></p>
</body>
</html>