javascript - How to write php include inside div with jquery -
i need write <?php include $this->_ script('admin/test.phtml'); ?>
using jquery within <div>
works.
i have researched , in responses recommend using load("file.php")
, not work in case, because framework , because of routes not work.
i tried , did not work, include inside script instead of doing in div:
<script> $('.btn-group').on('click', '#btn-edit', function(e) { e.preventdefault(); var id = $(this).attr('data-id'); $('#mymodal').modal({ backdrop: 'static' }); <?php $_get['id'] = id;?> var php = "<?php include $this->_script('admin/teste.phtml') ?>"; $('.modal-body').html(php); }); </script>
i tried code , did not work:
<script> $('.btn-group').on('click', '#btn-edit', function(e) { e.preventdefault(); var id = $(this).attr('data-id'); $('#mymodal').modal({ backdrop: 'static' }); <?php $_get['id'] = id;?> var php = "include $this->_script('admin/teste.phtml')"; $('.modal-body').html('<?php' + php + '?>'); }); </script>
the way work using jquery request page server server processing php again.
however, you've found, loading file doesn't work because file included dependencies not present.
the way solve problem (i happen using wordpress might change specific application of answer) this:
jquery:
jquery('.btn-group').on('click', '#btn-edit', function(e) { jquery.ajax({ type: "post", url: "/", //or whatever page want use load this. data: {"custom_php_action": "my_special_action"}, trycount : 0, retrylimit : 3, success: function(res){ jquery(".modal-body").html(res); //div response }, error: function(xhr, textstatus, errorthrown ) { if (textstatus == 'timeout') { this.trycount++; if (this.trycount <= this.retrylimit) { //try again jquery.ajax(this); return; } return; } } }); });
php (this whatever using build page change):
function my_custom_controller_function() { if ( isset($_post['custom_php_action']) && $_post['custom_php_action'] == 'my_special_action' ) { //tests our ajax conditions //my code here -- make sure whatever response echoed , not returned. //if function returns value, echo here. //example echo 'my response'; //example //example 2 $data = my_function(); echo $data; //example 2 exit(); //stop rest of page loading. } } add_action( 'wp', 'my_custom_controller_function' ); //control when ajax kicks in -- don't want firing -- or late.
this uses wordpress action hook fires after wordpress , plugins loaded, before theme files have been processed (if understand hook order correctly). depending on framework depend on how include code wanting use.
this how have solved dynamic loading of information. can piece solution example work since don't know class/object combination using locate document you're trying load/include , don't know details of platform you're building upon.
Comments
Post a Comment