Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms CodeIgniter POST/GET默认值_Forms_Codeigniter_Post - Fatal编程技术网

Forms CodeIgniter POST/GET默认值

Forms CodeIgniter POST/GET默认值,forms,codeigniter,post,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/corecreateMY_Input.p

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

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

所以我不必像这样编码

$a = $this->input->post("varname") ? 
     $this->input->post("varname") :
     "value-if-falsy"

您必须覆盖默认行为

application/core
create
MY_Input.php

class MY\u Input扩展CI\u Input
{
函数post($index=NULL,$xss\u clean=FALSE,$default\u value=NULL)
{
//检查是否已提供字段
如果($index==NULL和!空($\u POST))
{
$post=array();
//循环遍历完整的_POST数组并返回它
foreach(数组\u键($\u POST)作为$key)
{
$post[$key]=$this->_fetch_from_数组($_post,$key,$xss_clean);
}
退回$post;
}
$ret_val=$this->_fetch_from_数组($_POST,$index,$xss_clean);
如果(!$ret_val)
$ret_val=$default_值;
返回$ret_val;
}
}
然后在控制器中:

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

这对我很有效,我使用了@AdrienXL技巧

只需创建
应用程序/core/MY_Input.php
文件并调用父方法(您可以在CodeIgniter系统文件夹
系统/core/Input.php
中找到这些方法:

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class MY_Input extends CI_Input
{
    function post($index = NULL, $default_value = NULL, $xss_clean = FALSE)
    {
        $value = parent::post($index, $xss_clean);

        return ($value) ? $value : $default_value;
    }

    function get($index = NULL, $default_value = NULL, $xss_clean = FALSE)
    {
        $value = parent::get($index, $xss_clean);

        return ($value) ? $value : $default_value;
    }
}

您可以简化代码块,如下所示

public function post($index = NULL, $xss_clean = NULL, $default_value = NULL  )
{
    if( is_null( $value = $this->_fetch_from_array($_POST, $index, $xss_clean ) ) ){
        return $default_value;
    }
    return $value;
}

不久前我才发现我也可以使用
?:
,例如

$name = $this->input->post('name') ?: 'defaultvalue';

您可以在设置规则之前使用此代码

empty($_POST['varname']) ? $_POST['varname'] = 'value if empty' : 0;

如果您想将“value if falsy”设置为“”,那么它将自动执行此操作。您可以这样做,只需缩短,$a=$this->input->post(“varname”);$a=(empty($a))?“value if falsy”:$a;如果要将值保存到db,则可以在其中设置默认值。我以前已经创建了自己的输入类,以防没有内置的方法来解决此问题。谢谢。为什么要重写默认方法内容,而不调用
parent::post($index,$xss_clean)
?@fabsn如果我可以这样说的话,那只是为了教育目的。但是你是绝对正确的,我们可以在
parent::post()上做一个测试
,如果为false,则返回默认参数。我相信现在这应该是公认的答案。这是一种更简洁的方法,无需创建MY_输入类。公认的答案正是我当时的想法,上面的答案(由我提供)是非常新的。因此我保留了旧的公认答案,以表示尊重和感激,尽管现在我总是使用这个答案:-)可以
空($\u POST['varname'])和$\u POST['varname']=“value if false
无误运行吗?
empty($_POST['varname']) ? $_POST['varname'] = 'value if empty' : 0;