Welcome Guest, Not a member yet? Register   Sign In
Load a view from Javascript
#1

[eluser]tokyotech[/eluser]
When a user first comes to my page, I want to show a traditional form. After she fills out the form, I want to replace the whole form with an advanced inline-editing form. The traditional and the advanced form are stored in separate views. Since I don't want a page refresh when swapping the two forms, how do I load the advanced form via JS?

Note that I could put all the HTML in JS, but that would be ugly and a violation of Model View Controller.
#2

[eluser]darkhouse[/eluser]
Make an AJAX call to a controller that loads the view you want. I prefer jQuery, and it has a decent AJAX system.

Code:
//this is javascript, this forum doesn't allow script tags.
$('.form_loader').click(function(){
   $.post('ajax/load_form', {file: $(this).attr('href')}, function(data){
      $('form_container').html(data);
   });
   return false;
}

//html
<div id="form_container"></div>
<a href="simple_form" class="form_loader">Advanced Form</a>
<a href="advanced_form" class="form_loader">Advanced Form</a>

And then your PHP would be this:

Code:
class Ajax extends Controller {
   function Ajax(){
      parent::Controller();
   }

   function load_form($file){
      $this->load->view($file);
   }
}

I believe that should give you what you want.




Theme © iAndrew 2016 - Forum software by © MyBB