Preventing form submission again in Code Igniter

I am new to Code Igniter (as my first structure that I am learning).

I got this in my controller:

if ($this->form_validation->run() === FALSE) { $this->load->view('account/register', $data); } else { $data['message'] = $this->lang->line('account_created'); $this->register->insert_account(); $this->load->view('account/register_success', $data); } 

If the form is successfully validated, it just changes the view, but you can still click the refresh button and resubmit the form data - this is not a big problem for me, as I check if the fields are unique, but it would be better for me to prevent re-submission form data.

Normally in pure PHP I would use header("Location: ..."); but I am loading the view here so that after the redirect, it will not be able to access - right?

Do you have any suggestion?

+4
source share
2 answers

You can redirect to the same page, but also use codeigniters flashdata in the session library. In the submit form, specify flashdata with a successful message. Drag the form to the same page and display the flashdata success message. Page redirection reloads and prevents refresh.

controller

Do it with form success

 $this->session->set_flashdata("success", $strMessage); redirect("account/register"); 

view

This will display a success message on the form page.

 if($this->session->flashdata("success") !== FALSE) { echo "<div class=\"formSuccess\">" . $this->session->flashdata("success") . "</div>\n"; } 
+8
source

Ajax is the view you need in a div. See what I mean? change the contents of the page, but not the page.

-one
source

Source: https://habr.com/ru/post/1395836/


All Articles