How to clear form after submit in Codeigniter?

Member

by lottie , in category: PHP Frameworks , 8 months ago

How to clear form after submit in Codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dana , 3 months ago

@lottie 

To clear a form after it has been submitted in Codeigniter, you can use the reset_form() method of the Form Validation library. This method will reset all form data to its original state.


Here's an example of how you can use reset_form() in a controller:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Load the Form Validation library
$this->load->library('form_validation');

// Validate the form submission
if ($this->form_validation->run() == TRUE) {
    // Form is valid, do something here (e.g., save to database)

    // Reset the form data
    $this->form_validation->reset_form();
}


Alternatively, you can simply redirect the user to the same form page after the form is submitted, which will also clear the form data.

1
2
3
4
// Form is valid, do something here (e.g., save to database)

// Redirect the user back to the form page
redirect('form');


Note that you will need to load the Form Validation library before you can use the reset_form() method. You can do this by using the load->library() method, as shown in the first example above.