How Parsing javascript vars to PHP in Datatable

How Parsing javascript vars to PHP in Datatable

ANdraANdra Posts: 2Questions: 1Answers: 0
edited July 2015 in Free community support

Hi all, i'm new in datatables and sorry for my bad english.

i really need your help to help solve my problem, i;m stuck on this.

So, my code is all in one page :

 $( function () {
 var oTable;
  function fnFormatDetails( nTr ) {
 var aData = oTable.fnGetData(nTr);
 sOut = "<?php foreach($this->m_rumah_sakit->getStyleAndCount(280)->result() as  $style){ 
            echo $style->keterangan; ?>  
           <br /> 1. Yes : ( <i><?php echo ($style->yes)?$style->yes:'0'; ?> Ulasan </i>) 
           <br /> 2. No : ( <i><?php echo ($style->no)?$style->no:'0'; ?> Ulasan</i> ) 
           <br /> 3. Not Sure : ( <i><?php echo ($style->not_sure)?$style->not_sure:'0'; ?> Ulasan</i> ) <br /><?php } ?>";

 return sOut;
 }

     $(document).ready ( function() {
oTable = $('#tSortable').dataTable({
            "bProcessing": true,
            "bServerSide": true,
            "aaSorting": [[1,'asc']],
            "oLanguage": {"sProcessing": "<img src=" + vars.loader + ">"},
            //"sAjaxSource": "../details_col.php",      
            "sAjaxSource" : base + "cms-ajax-data",
            "fnServerParams" : function ( aoData ) {
               aoData.push(
                { "name": "p", "value" : "" },
                { "name": "hRating", "value" : "hospital" }
                ,{ "name": "c", "value" : vars.column }
                ,{ "name": "iC", "value" : vars.indexColumn }
                ,{ "name": "t", "value" : vars.table }
                ,{ "name": "w", "value" : vars.where }
                ,{ "name": "imp", "value" : vars.implode }
               );
            },               
            "aoColumnDefs": [{"bSortable":false,"bSearchable":false, "sClass": "hidden", "aTargets":[1]}]
});

$('#tSortable tbody td img').live('click',function(){
    var nTr = this.parentNode.parentNode;

       if(this.src.match('details_close')){
        this.src = "..//details_open.png";
        oTable.fnClose(nTr);
    }else
        this.src = "../details_close.png";
        oTable.fnOpen(nTr,fnFormatDetails(nTr),'details');
    }

});
  });

And all works like charms! the only problem is : how i can get variable aData[2] in PHP script?
i mean, how to call it like this in my PHP code :
foreach($this->m_rumah_sakit->getStyleAndCount("+aData[2]+")->result();

Hope u understand what i mean. Thanks for any advise

Answers

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin

    The PHP is executed at the server - i.e. before the Javascript. There is simply no way to execute PHP at the client-side when that function itself is executed.

    You have two options:

    1. Make an Ajax request to a PHP script that will create the string you need to display
    2. Do the formatting in Javascript.

    Allan

  • ANdraANdra Posts: 2Questions: 1Answers: 0
    edited July 2015

    Thanks for your reply allan, so, can you tell me how i suppose to do next if i choose to formating in javascript.

    I've try to make an ajax request but still does't works:

    var var_data = aData[2];
    $.ajax({
      type: 'GET',
      data: {var_PHP_data: var_data},
      success: function(data){                    
        $(oTable.fnOpen(nTr,sOut,'details')).html($(data).find('#result').html());
      }
     });
    

    It just give me the empty PHP variabel

    <?php if (isset($_GET["var_PHP_data"])){ echo $_GET["var_PHP_data"]; }?>

    And from now on, i think the option 2 will be my last choice :D

    thanks

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin

    You'r $.ajax call doesn't appear to have a url option. You probably need one.

    Allan

This discussion has been closed.