【问题标题】:Generic PHP form processor通用 PHP 表单处理器
【发布时间】: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 =&gt; $submittedValue)
  • @RiggsFolly $_REQUEST 也包括 $_COOKIE
  • @Mike 不在我的 PHP 上 request_order = "GP" 但你说得对,它可能是,我忘记了

标签: php forms post get request


【解决方案1】:

您可以在应用程序的主文件上循环一次并将$_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
        )

)

【讨论】:

  • 如果用户发布一个数组输入怎么办?
  • @Cemal 是的,它确实处理数组。我用$input_values 的输出更新了答案
  • 如果你使用 echo 而不是 print_r,我想你会得到一个 Notice: Array to string conversion in...
  • 当然,开发人员应该知道他期望的结果类型。 echo $_GET/POST['array']时也会出现同样的错误。
  • 好吧,他提到它可以是任何类型,因此您的代码将无法处理它。
猜你喜欢
  • 2021-12-05
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 2015-07-09
  • 2015-05-17
  • 1970-01-01
  • 2012-02-15
  • 1970-01-01
相关资源
最近更新 更多