[英]Generic PHP form processor
我正在尝试创建一个优雅的表单处理器,可以接收POST或GET并循环显示值并显示它们。 我想使它尽可能简单/优雅,并且似乎不能为了支持REQUEST而消除GET / POST。 这段代码输出了一个额外的值对,我显然不明白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>
您可以在应用程序的主文件上循环一次,并将$_GET
和$_POST
存储在单个变量内(它的作用类似于$_REQUEST
但没有$_COOKIES
)。 然后你可以像这样使用它
<?php
$input_values = array();
$gpcs = array_merge($_POST, $_GET);
foreach ($gpcs as $key => $value) {
$input_values[$key] = $value;
}
// Then you can use anywhere in your application like this
echo isset($input_values['username'])? $input_values['username']:'';
echo isset($input_values['password'])? $input_values['password']:'';
echo isset($input_values['user_type'])? $input_values['user_type']:'';
?>
编辑: 表格和结果
<form action="" method="POST">
<input type="text" name="demo" value="Demo"><br/>
<input type="text" name="demo2" value="Demo 2"><br/>
<input type="text" name="array[]" value="Array 1"><br/>
<input type="text" name="array[]" value="Array 2"><br/>
<input type="text" name="array[]" value="Array 3"><br/>
<input type="text" name="array[]" value="Array 4"><br/>
<input type="submit">
</form>
Array
(
[demo] => Demo
[demo2] => Demo 2
[array] => Array
(
[0] => Array 1
[1] => Array 2
[2] => Array 3
[3] => Array 4
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.