How to keep form data after submitting using Codelgniter

Asked

Viewed 155 times

0

I am beginner with the use of Codelgniter Framework, I have a doubt how to keep the data of my forms after submitting the page, I am creating a form with many fields to be filled, if the user leaves a blank field or misspelling it will generate an error msg, I wanted the user to have the option to return to the form and rescue what was typed.

$this->form_validation->set_rules('nome', 'NOME', 'required|min_length[3]|max_length[40]');
    $this->form_validation->set_rules('data_nasc', 'DATA DE NASCIMENTO', 'required');
    $this->form_validation->set_rules('rg', 'RG', 'required|exact_length[9]|numeric');
    $this->form_validation->set_rules('cpf', 'CPF', 'required|exact_length[11]|numeric');
    $this->form_validation->set_rules('sexo_pessoa', 'SEXO DO CLIENTE', 'required|numeric');
    $this->form_validation->set_rules('rua', 'RUA', 'required|min_length[4]|max_length[20]|trim');
    $this->form_validation->set_rules('numero', 'NUMERO', 'required|max_length[5]|numeric');
    $this->form_validation->set_rules('cep', 'CEP', 'required|exact_length[8]|numeric');
    $this->form_validation->set_rules('estado', 'ESTADO', 'required|numeric');
    $this->form_validation->set_rules('cidade', 'CIDADE', 'required|numeric');
    $this->form_validation->set_rules('bairro', 'BAIRRO', 'required|numeric');
    $this->form_validation->set_rules('tel1', 'TELEFONE 1', 'required|min_length[10]|max_length[11]|numeric');      
    $this->form_validation->set_rules('cat_tel1', 'CATEGORIA DO TELEFONE 1', 'required|numeric');       
    $this->form_validation->set_rules('nome_animal', 'NOME DO ANIMAL', 'alpha|required|min_length[3]|max_length[20]');
    $this->form_validation->set_rules('categoria_animal', 'CATEGORIA DO ANIMAL', 'required|numeric');
    $this->form_validation->set_rules('raca', 'RAÇA', 'required|numeric');
    $this->form_validation->set_rules('sexo_animal', 'SEXO DO ANIMAL', 'required|numeric');
    $this->form_validation->set_rules('cor', 'COR', 'required|numeric');
    $this->form_validation->set_rules('porte', 'PORTE', 'required|numeric');
    $this->form_validation->set_rules('data_nasc_animal', 'DATA DE NASCIMENTO DO ANIMAL', 'required');

    if($this->form_validation->run() == FALSE){
        if(validation_errors()){
            die('<p> <a href="'.base_url('cadastro_cliente').'"> Clique aqui </a> para voltar</p>'.validation_errors());
        }
    }       

1 answer

0

You need two things to make the values come back filled in a form that went wrong:

1°) Recall the view passing a $data with the value of these fields you have gone through validation. 2°) Have programmed the form fields with value= receiving the function set_data(), whether or not the value exists, for when the form is reset or not.

If all the fields came by POST, you ride quickly one $data picking up PHP values $_POST. If some variable is not in the POST, you have to add in hand. Logo:

if($this->form_validation->run() == FALSE){
    if(validation_errors()){
        $data = $this->input->post();

        // Adiciona uma variável manualmente no $data
        $data['erros_do_form'] = validation_errors();

        // Usuário volta para o mesmo lugar onde estava
        $this->load->view('nome_da_view', $data);
    }
}  

In the view, display to the user the errors occurred through the variable (array) erros_do_form.

Still in the view, you need to prepare the inputs to receive or not value, that is, the same form will be called zeroed or filled with values. Example for animal name field:

// null é o valor default aqui, mas poderia ser 'Lulu'
<input name="nome_animal" type="text" value="<?=set_value('nome_animal', null)?>">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.