CodeIgniter - display success or failure message

I am wondering if anyone can tell me how they handle success / failure messages in CodeIgniter.

For example, what I do when the user subscribes to my site is what happens in the controller

if (!is_null($data = $this->auth->create_user( $this->form_validation->set_value('username'), $this->form_validation->set_value('password') ))) { // Redirect to the successful controller redirect( 'create-profile/successful' ); } else { // Redirect to the unsuccessful controller redirect( 'create-profile/unsuccessful' ); } 

Then in the same controller (create-profile) I have 2 methods that are similar to the following

 function successful() { $data['h1title'] = 'Successful Registration'; $data['subtext'] = '<p>Test to go here</p>'; // Load the message page $this->load->view('message',$data); } 

The problem is that I can just go to the .com / create-profile / successful site and it will display the page.

If someone could show me the best way to handle this, it would be quite helpful.

Greetings

+4
source share
3 answers

there is a reason why you are not using this:

 if (!is_null($data = $this->auth->create_user( $this->form_validation->set_value('username'), $this->form_validation->set_value('password') ))) { $data['h1title'] = 'Successful Registration'; $data['subtext'] = '<p>Test to go here</p>'; // Load the message page $this->load->view('message',$data); } else { $data['h1title'] = 'Unsuccessful Registration'; $data['subtext'] = '<p>Test to go here</p>'; // Load the message page $this->load->view('message',$data); } 

Hello, Stefan

+4
source

Before redirecting, you can install flashdata :

 $this->session->set_flashdata('create_profile_successful', $some_data); redirect( 'create-profile/successful' ); 

 function successful(){ if( FALSE == ($data = $this->session->flashdata('create_profile_successful'))){ redirect('/'); } $data['h1title'] = 'Successful Registration'; $data['subtext'] = '<p>Test to go here</p>'; // Load the message page $this->load->view('message',$data); } 
+6
source

Instead of redirecting, just show different views.

Here is a sample code:

 if ($this->input->server('REQUEST_METHOD') == 'POST') { // handle form submission if (!is_null($data = $this->auth->create_user( $this->form_validation->set_value('username'), $this->form_validation->set_value('password') ))) { // show success page $data['h1title'] = 'Successful Registration'; $data['subtext'] = '<p>Test to go here</p>'; // Load the message page $this->load->view('message',$data) } else { // show unsuccessful page $data['h1title'] = 'Unsuccessful Registration'; $data['subtext'] = '<p>Test to go here</p>'; // Load the message page $this->load->view('message',$data) } } else { // show login page $data['h1title'] = 'Login'; $data['subtext'] = '<p>Test to go here</p>'; // Load the message page $this->load->view('login',$data) } 
+2
source

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


All Articles