2

New to HMVC in Codeigniter.

Dynamic form allows new "segments" to be created when a user clicks a link.

Currently the segment html & php is in module/view/segment_view.php. The view requires dynamic php variables as well.

How can I "load the view" using jQuery? I understand I can't directly load a view from jQuery but am lost on how to structure the controller for an AJAX call. Seems this may not be a strength of Codeigniter?

EDIT:
The "segment" is inside a form. The form always has 1 segment. jQuery can add more segments. Each segment requires php arrays for form inputs and validation.

Here is a pared down version of the form_view file

for ($i=1; $i<=$number_of_segments; $i++) {

    $data['location'] = array(
        'name'  =>  "segment[$i][location]",
        'value' =>  set_value("segment[$i][location]"),
        'maxlength' =>  '255',
        'size'  =>  '30'
    );  
 ?>
    <div class="area">
        <div class="row"><h2>Segment <?php echo $i;?></h2></div>

        <?php 
        $this->load->view('module/segment_view', $data);
        ?>

    </div> <!--/div class="area"-->
<?php
}    //end for
?>

OK, so this isn't really separating application logic from presentation, now is it? Ha ha. So what is the best way to structure this so the code is HMVC and can be accessed from a php controller/view as well as jQuery?

1 Answer 1

3

You can't call view files directly by URL, you need to call a controller method that loads the view.

If you want to do some "ajax only" stuff, you may use this function:

if ($this->input->is_ajax_request()) // Do ajax stuff

Examples would be loading view fragments or json. You don't have to make "ajax only" methods in your controller, you can use this function from within your existing controller methods to do something different if it's an ajax request.

Not sure if this resolves anything for you, provide some code and more context of what you're trying to do and we can give you some clearer advice.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks and I believe that is_ajax_request is what I am looking for but I am still trying to fully grasp the proper way to implement complex HMVC. Please check my edits if you can. Thanks again.
@ChristopherIckes: I'm sorry to admit, but after seeing your edit I still can't figure out what you're trying to do, or what you're having issues with. There's really nothing at all related to HMVC or jQuery as far as I can tell.
Ha ha, that's true! But is_ajax_request() was the answer. I separated the above question in controller & view and removed the view logic. Now I can request the pieces I need using is_ajax_request(). Works like a charm now that the idea is developed and the code is HMVC, not gobbledy gook. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.