forms - CodeIgniter POST/GET 默认值

标签 forms codeigniter post

如果 POST/GET 数据为空/假,我可以为它设置默认值吗,比如

$this->input->post("varname", "value-if-falsy")

?

所以我不必像这样编码
$a = $this->input->post("varname") ? 
     $this->input->post("varname") :
     "value-if-falsy"

最佳答案

您必须覆盖默认行为。

application/core创建 MY_Input.php

class MY_Input extends CI_Input
{
    function post($index = NULL, $xss_clean = FALSE, $default_value = NULL)
    {
        // Check if a field has been provided
        if ($index === NULL AND ! empty($_POST))
        {
            $post = array();

            // Loop through the full _POST array and return it
            foreach (array_keys($_POST) as $key)
            {
                $post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
            }

            return $post;
        }

        $ret_val = $this->_fetch_from_array($_POST, $index, $xss_clean);
        if(!$ret_val)
            $ret_val = $default_value;

        return $ret_val;
    }
}

然后在您的 Controller 中:
$this->input->post("varname", "", "value-if-falsy")

关于forms - CodeIgniter POST/GET 默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28622113/

相关文章:

python - 使用 Python mechanize 通过登录屏幕

php - 在内联 CSS 中使用 $_POST

php - 我无法将日期从 codeigniter 插入到 mysql DATE 列

mysql - SQL 语句不返回结果

mysql - 如果该行具有相同的值,则将光标移动到下一个

http - 如何http :post file with httpc:request in erlang?

jQuery GET 不发送浏览器中的 IE

forms - Meteor:如何在一组表单字段中接受用户输入

ajax - 无法从ajax请求中获取Golang中的post参数

html - 样式化的搜索栏在 IE7 中完全消失