title: How to redirect from controller to view in CodeIgniter?
body: <p>It was really hard to formulate the title of this question. I`ll start with a code snippet:</p>
  
  <pre><code>[..]
  &lt;a href="#" id="register-link"&gt;register&lt;/a&gt;
  [..]
  &lt;script&gt;
  $(document).ready(function(){
      $('#register-link').on('click',function(){
          $.get(baseurl + 'index.php/auth/register', function(response){
              $('#popup-div').html(response);
          });
      });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>controller method auth/register ends with:</p>
  
  <pre><code>$this-&gt;load-&gt;view('auth/register_form', $data);
  </code></pre>
  
  <p>The basic idea is that when user clicks on "register" in menu a popup show up instead of loading a new page. Registration works. </p>
  
  <p>However, there are two ways how auth/register method can end:</p>
  
  <pre><code>function register()
      {
          // a bunch of $this-&gt;form_validation-&gt;set_rules();
          // and other server side logic
          if ($this-&gt;form_validation-&gt;run()) {    
          // server side logic
              redirect('auth');
          } else{
          //getting same $data
              $this-&gt;load-&gt;view('auth/register_form', $data)
          }
      }
  </code></pre>
  
  <p>So the problem is that if I get validation errors (form validation doesn`t run and I get html response) a new page loads with form showing errors instead of doing that in the popup. If registration is successful I get new page reload just to display to user that registration was successfull which is annoying. So I want to check if controller redirects and to pass that redirect url into the same div.</p>
  
  <p>I looked up same similar questions but can not get it working. So far I have this:</p>
  
  <pre><code>$(document).ready(function(){
      $('#register-link').on('click',function(){
          $.get(baseurl + 'index.php/auth/register', function(response){
              if (response.redirect) {
                  //alert(response.redirect);
                  $.get(response.redirect, function(response2){
                      $('#popup-div').html(response2);                    
                  });
              }
              else {
                  $('#popup-div').html(response);
              }
          });
      });
  });
  </code></pre>
  
  <p>but nothing changes and I do not get any errors. If I understand the problem correctly then call is made just once so I will always get the view. I tried to remove  <code>$(document).ready(function(){ ... });</code> part but still nothing. If I will try to attach ajax call to every "submit" I (think) will mess up the validations. </p>
  
  <p><strong>UPDATE:</strong>
  From comments below I got that it could be easier to work inside tank auth controller. First I`ll show full sequence how Tank auth redirects. Inside auth/register it is actually</p>
  
  <pre><code>$this-&gt;_show_message($this-&gt;lang-&gt;line('auth_message_registration_completed_1'));
  </code></pre>
  
  <p>where:</p>
  
  <pre><code>function _show_message($message)
      {
          $this-&gt;session-&gt;set_flashdata('message', $message);
          redirect('/auth/');
      }
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>function index()
      {
          if ($message = $this-&gt;session-&gt;flashdata('message')) {
              $this-&gt;load-&gt;view('auth/general_message', array('message' =&gt; $message));
          } else {
              redirect('/auth/login/');
          }
      }
  </code></pre>
  
  <p>The idea behind it is that _show_message is called in 17 different situation with different message to display. I want to handle all of those situation within my popup div. I tried to change:</p>
  
  <pre><code>$this-&gt;_show_message_JSON($this-&gt;lang-&gt;line('auth_message_registration_completed_1'));
  </code></pre>
  
  <p>where:</p>
  
  <pre><code>function _show_message_JSON($message)
      {
          $this-&gt;session-&gt;set_flashdata('message', $message);
          $data['redirect'] = '1';
          header('Cache-Control: no-cache, must-revalidate');
          header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
          header('Content-Type: application/json');
          echo json_encode($data);
      }
  </code></pre>
  
  <p>and handle logic in JS:</p>
  
  <pre><code>$(document).ready(function(){
      $('#register-link').on('click',function(){
          $.get(baseurl + 'index.php/auth/register', function(response){
              if(typeof response == 'object'){
                  //alert(response);
                  $.get(baseurl + 'index.php/auth', function(response2){
                      $('#popup-div').html(response2);                    
                  });
              }
              else {
                  $('#popup-div').html(response);
              }
          });
      });
  });
  </code></pre>
  
  <p>However what I get if validation is ok is:</p>
  
  <pre><code>{"redirect":"1"}
  &lt;form action="http://localhost/test4/index.php/auth/register" method="post" accept-charset="utf-8"&gt;
  &lt;table&gt;
          &lt;tr&gt;
          &lt;td&gt;&lt;label for="username"&gt;Username&lt;/label&gt;&lt;/td&gt;
          &lt;td&gt;&lt;input type="text" name="username" value="user21" id="username" maxlength="20" size="30"  /&gt;
  &lt;/td&gt;
          &lt;td style="color: red;"&gt;&lt;/td&gt;
      &lt;/tr&gt;
  [...]
  </code></pre>
  
  <p>which is basically the source code except with attached first line. </p>