phpformscodeigniterflashreloaddata

form values lost if captcha error in codeigniter php


I have created a website in php, the website has a signup form and google captcha in it. If the captcha is correct the form will submit successfully. If the captcha is wrong the same page will reload:

$this->session->set_flashdata('message', 'Captcha Error.');
redirect(base_url('signup'), 'Location');

The problem is the page is reloading with empty values, the user entered inputs are gone, can anyone please tell me what am i missing in my code?

Thanks in advance.


Solution

  • You can save your form value in session, and autofill the form based on data in session.

    //set formdata to session
    $data_session = array(
        'name' => $name,
        'email' => $email
    );
    $this->session->set_userdata('form_data', $data_session);
    

    And set the data when you load the register page, and set the value to your view.

    //load session data if exist
    $data['name'] = null;
    $data['email'] = null;
    if ($this->session->userdata('form_data') != null)
    {
        $data['name'] = $this->session->userdata('form_data')['name'];
        $data['email'] = $this->session->userdata('form_data')['email'];
    }
    $this->load->view('register_view',$data);
    

    Dont forget to remove the session if signup is successful.

    $this->session->unset_userdata('form_data');
    

    Alternatively you can also use HTTP method and POST / GET the data to the new page (dont use HTTP GET if you want to send the password).