【发布时间】:2018-02-19 22:33:36
【问题描述】:
我正在尝试创建一个优雅的表单处理器,它可以接收 POST 或 GET 并循环遍历值并显示它们。我想让它尽可能简单/优雅,并且似乎无法消除 GET/POST 以支持 REQUEST。这段代码输出了一个额外的值对,我显然不明白 GET/POST 与 REQUEST 的关系。表单包含使用 POST 时的先前 GET 信息(因此它们似乎重叠),我不确定如何轻松地不包含 cookie(我认为就是这样)值。我知道 REQUEST 不是做到这一点的最佳方式,但仍然想知道是否没有一种很酷的方式来以这种方式使用它。接受其他建议。
<h1>
Welcome to the Flexiform! AKA "The Formerator"
</h1>
<p>
This form should process any combination of inputs... this iteration doesn't handle identically-named inputs other than checkboxes. Would like to figure that out.
</p>
<?php
if ($_GET || $_POST)
//if ($_REQUEST) // why can't I use this?... it seems to process the incoming page as GET on firstload...
{
echo ("<h2>Way to '" . $_SERVER['REQUEST_METHOD'] . "' some! </h2>");
foreach($_REQUEST as $submittedName => $submittedValue)
{
if (is_array($submittedValue)) // for checkboxes with more than one selected value
{
$submittedValue = implode(', ', $submittedValue);
}
echo "For the <b>" . $submittedName . " input, you submitted: :</b> " . $submittedValue."</br>";
}
}
else
{
echo ("<h2>Please submit one of the two forms to see some results.</h2>");
}
?>
<hr><hr>
<br>
GET SOME!
<form action= "" method = "get" >
<input type = "text" name = "name" value = "GIcabad" />
<input type = "text" name = "name2" value = "GIcabad2" />
<input type = "text" name = "name3" value = "GIcabad3" />
<input type = "text" name = "name" value = "GIcabadbb" />
<input type = "text" name = "name2" value = "GIcabad2bb" />
<input type = "text" name = "name3" value = "GIcabad3bb" />
orange:<input type = "checkbox" name = "colors[]" value = "orange" checked />
red:<input type = "checkbox" name = "colors[]" value = "red" />
pink:<input type = "checkbox" name = "colors[]" value = "pink" checked />
<input type = "submit" />
</form>
<br>
POST SOME!
<form action = "" method= "post" >
<input type = "text" name = "name" value = "PIcabad" />
<input type = "text" name = "name2" value = "PIcabad2" />
<input type = "text" name = "name3" value = "PIcabad3" />
<input type = "submit" />
</form>
【问题讨论】:
-
这里有问题吗?
-
$_REQUEST是$_POST+$_GET因此,如果您使用 GET 和查询字符串访问此页面,它也会出现在您的$_REQUEST中 -
你可以试试
foreach(array($_GET, $_POST) as $submittedName => $submittedValue) -
@RiggsFolly
$_REQUEST也包括$_COOKIE -
@Mike 不在我的 PHP 上
request_order = "GP"但你说得对,它可能是,我忘记了
标签: php forms post get request