CodeIgniter Forums

Full Version: CI + ajax
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I wanted to ask if is possible to call a modal and get a value from a ajax get request. Trying to do this gives me a 500 Internal Server Error.

Thanks.
No, you can't call a model, library, helper, etc, via the URL. You can only call controllers. So create a new controller, or add a method to and existing one, that will load your model and return your data when the ajax posts to it.
you can only call controllers with arguments.
It is possible.
TS is asking create Modal, not asking get from Model.

You can use it in view like this:
HTML and JS:
PHP Code:
<div class="bs-example">
 <!-- 
Button HTML (to Trigger Modal) -->
 <
a href="#myModal" class="btn btn-lg btn-primary" data-toggle="modal">Launch Demo Modal</a>

 <!-- 
Modal HTML -->
 <
div id="myModal" class="modal fade">
 <
div class="modal-dialog">
 <
div class="modal-content">
 <
div class="modal-header">
 <
button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
 <
h4 class="modal-title">Confirmation</h4>
 </
div>
 <
div class="modal-body">
 <
div id="ajax_content"></div>
 <
p>Do you want to save changes you made to document before closing?</p>
 <
class="text-warning"><small>If you don't save, your changes will be lost.</small></p>
 </div>
 <div class="modal-footer">
 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
 <button type="button" class="btn btn-primary">Save changes</button>
 </div>
 </div>
 </div>
 </div>
 </div>

 <script type="text/javascript">
 $(document).ready(function() {
 $.get('
<?php echo site_url(); ?>sample_modal/get_ajax_result', function(data) {
 $('#ajax_content').html(data);
 });
 });
 </script> 

Controller:
Sample_modal method get_ajax_result
PHP Code:
public function get_ajax_result() {
 echo 
'Content from ajax';
 exit();