Welcome Guest, Not a member yet? Register   Sign In
Difference between Redirect en load-view
#1

[eluser]Unknown[/eluser]
After a form submit, and the data is wrong.. what's the difference between using $this->load->view('bla') or redirect('blog/post')

Here is my full code for now..
Code:
function comment($id){
  $this->load->library('form_validation');
  $this->form_validation->set_rules('name','Name','trim|required');
  $this->form_validation->set_rules('email','Email','trim|required|valid_email');
  $this->form_validation->set_rules('comment','Comment','required');

  if($this->form_validation->run() == FALSE){
   redirect('blog/post/'.$id);
  }


Thanks!
#2

[eluser]Harold Villacorte[/eluser]
If you want to access Ci's form validation errors use load->view(). If you want flashdata use redirect().
#3

[eluser]Aken[/eluser]
Have you read the user guide descriptions of each item? redirect() sends a Location header and literally redirects the browser to a specific URL. $this->load->view() loads a view file and sends it to the output class to be displayed to the browser.
#4

[eluser]Unknown[/eluser]
[quote author="Harold Vilacorte" date="1359088135"]If you want to access Ci's form validation errors use load->view(). If you want flashdata use redirect().[/quote]

Well i want to load the view.. but it's a dynamic view.. it loads a current post

-> blog/post/4 so how would i go back to that page... thanks!
#5

[eluser]Harold Villacorte[/eluser]
This would be the normal way of doing it:

Code:
function comment($id){
  $this->load->library('form_validation');
  $this->form_validation->set_rules('name','Name','trim|required');
  $this->form_validation->set_rules('email','Email','trim|required|valid_email');
  $this->form_validation->set_rules('comment','Comment','required');

  if($this->form_validation->run() == FALSE){
    $this->load->view('the_same_view_file');
  }

Then somewhere in the view file put something like this:

Code:
<?php if (validation_errors ()) : ?>
  <div>
    &lt;?php echo validation_errors (); ?&gt;
  </div>
&lt;?php endif; ?&gt;

Else redirect:

Code:
else {
  $this->session->set_flashdata('type_of_message', 'Your success message here.');
  redirect (base_url() . 'whatever_url/' );
}

Then to echo the flashdata in the next view file:

Code:
&lt;?php if ($this->session->flashdata('type_of_message')) : ?&gt;
  <div>
    &lt;?php echo $this->session->flashdata('type_of_message'); ?&gt;
  </div>
&lt;?php endif; ?&gt;

Or something to that effect.
#6

[eluser]Harold Villacorte[/eluser]
I might have posted some erroneous code here. Anyway just to not steer anyone in the wrong direction here is how I retrieve flashdata(). Obviously there is assignment in condition here, hard habit to break:

In the controller:

Code:
$data = array();
    if ($message_success = $this->session->flashdata ('message_success')) {
      $data['message_success'] = $message_success;
    }
    if ($message_error = $this->session->flashdata ('message_error')) {
      $data['message_error'] = $message_error;
    }
    if ($message_notice = $this->session->flashdata ('message_notice')) {
      $data['message_notice'] = $message_notice;
    }
    $this->load->view ('core_messages', $data);

In the view:

Code:
&lt;?php if (isset ($message_success)) : ?&gt;
  <div class="alert-box success">
     &lt;?php echo $message_success; ?&gt;
    <a href="" class="close">&times;</a>
  </div>
&lt;?php endif; ?&gt;
  &lt;?php if (isset ($message_error)) : ?&gt;
    <div class="alert-box alert">
      &lt;?php echo $message_error; ?&gt;
      <a href="" class="close">&times;</a>
   </div>
&lt;?php endif; ?&gt;
&lt;?php if (isset ($message_notice)) : ?&gt;
  <div class="alert-box">
    &lt;?php echo $message_notice; ?&gt;
    <a href="" class="close">&times;</a>
  </div>
&lt;?php endif; ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB