简体   繁体   中英

how to set a default value in codeigniter?

is there any sugestion to store default value that is posted to database from form html? my aplication is using mysql as the rdmbs and codeigniter for backend process.

right now, i just using html method at the submit form.

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

at the html page and set the value there. 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?

There is no such a way but you can keep it in encoding format using

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

cut "1231231231" in controller like below to get the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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