简体   繁体   English

如何在codeigniter中设置默认值?

[英]how to set a default value in codeigniter?

is there any sugestion to store default value that is posted to database from form html? 是否有任何存储从html表单发布到数据库的默认值的建议? my aplication is using mysql as the rdmbs and codeigniter for backend process. 我的应用是使用mysql作为后端进程的rdmbs和codeigniter。

right now, i just using html method at the submit form. 现在,我只是在提交表单上使用html方法。

<input type="hidden" id="" name="" value="">

at the html page and set the value there. 在html页面上,然后在此处设置值。 but the value is visible when you open the source code in any other browser. 但是在任何其他浏览器中打开源代码时,该值都是可见的。 is there any method to store value that is not visible from user in codeigniter? 有什么方法可以在Codeigniter中存储用户看不见的值?

There is no such a way but you can keep it in encoding format using 没有这样的方法,但是您可以使用

base64_encode() BASE64_ENCODE()

<input type="hidden" name="id" value="<php echo base64_encode('1231231231'.$id); ?>">

"1231231231" just for additional security which will preprend with the id “ 1231231231”仅用于附加安全性,该安全性将以id开头

cut "1231231231" in controller like below to get the id 在如下所示的控制器中剪切“ 1231231231”以获取ID

    function abc() {
    if (isset($_POST['id']) && !empty($_POST['id'])) {
        $id = substr(base64_decode($_GET['id']), 10); // to remive extra string
        if (!empty($id)) {
            // you  get id here
        } else {
            //if someone attempt to tamper with the id then it will retrive blank
        }
    } else {
        //redirect or show error
    }
}

If your form is posting the data on the same method wherefrom it is rendered then the best approach is that; 如果您的表单是以相同的方法发布数据的,那么最好的方法就是从该方法发布数据。 keep the value in the method itself and use it after form submit. 将值保留在方法本身中,并在表单提交后使用。

public function edit() { 
     $id = 12;
     if ($this->form_validation->run()) {
         $dataComm    = array(
                            'id'  => $id
                            'name'       => $this->input->post('name') 
                            );
                            $result_comm = $this->Common_model->insert_data('cw_franchise_commission', $dataComm);
     }
     $this->data['title'] = "Edit User";
     $this->load->view('edit', $this->data);
 }

And if your form action is another method then you can encode the value and decode it after form submission. 如果您的表单操作是另一种方法,则可以对值进行编码,并在提交表单后对其进行解码。

To Encode 编码

bin2hex(base64_encode($id))

To Decode 解码

base64_decode(hex2bin($this->input->post('id')));

One more thing if you are using hidden inputs then you should name it something confusing not clear at all. 还有一件事,如果您使用隐藏的输入,那么您应该给它起个混淆不清的名字。 Otherwise, anyone can understand the purpose of the field. 否则,任何人都可以理解该领域的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM