-3
function retrieveHasilRawatJalan(row, kd_klp) {
   var hasil_rawat_jalan2 = <?php echo
   Modules::run("lab/get_row_content_from_lab_code","HL-024") ?>; 
}

how to replace "HL-024" with variable kd_klp?

i get an error if i use it this way

 function retrieveHasilRawatJalan(row, kd_klp) {
       var hasil_rawat_jalan2 = <?php echo
       Modules::run("lab/get_row_content_from_lab_code",?>kd_klp<?php) ?>; 
 }

the error say Parse error: syntax error, unexpected '?>'

if my question isnt clear, please ask Thanks^^

UPDATE

Before add AJAX

 /**
  *
  * @param {type} id
  * @returns {undefined}
  */
 function retrieveHasilRawatJalan(row) {
   var hasil_rawat_jalan2 = <? php echo Modules::run("lab/get_row_content_from_lab_code", "HL-003") ?> ;

   //var hasil_rawat_jalan2 = <?php //echo Modules::run("lab/get_row_content_from_lab_code", row)                                     ?>;
   var number_of_row = parseInt(Object.size(hasil_rawat_jalan2));
   var row_start = parseInt(row);

   addNewRow(number_of_row);
   var row_end = (number_of_row + row_start);

   j = 1;
   for (i = row_start; i <= row_end; i++) {
     document.getElementById('SUBKLP[' + i + ']').value = hasil_rawat_jalan2[j]['sub_klp'];
     document.getElementById('NAMA[' + i + ']').value = hasil_rawat_jalan2[j]['name_of_inspection'];
     document.getElementById('KODE[' + i + ']').value = hasil_rawat_jalan2[j]['inspection_id'];
     document.getElementById('HASIL[' + i + ']').value = hasil_rawat_jalan2[j]['result'];
     //document.getElementById('NILAI_NORMAL[' + i + ']').value = hasil_rawat_jalan2[j]['normal_result'];
     document.getElementById('NILAI_NORMAL[' + i + ']').value = 'null';
     document.getElementById('SATUAN[' + i + ']').value = hasil_rawat_jalan2[j]['measure_unit'];
     document.getElementById('KDKLP[' + i + ']').value = hasil_rawat_jalan2[j]['klp_id'];
     j++;
   }
   console.log("row start: " + row_start + ", row end:" + row_end + ", column length: " + number_of_row);
 }

After Add AJAX

/**
     * test ajax
     * @param {type} row
     * @returns {undefined}
     */
    function retrieveHasilRawatJalan2() {

      var row = "HL-003";
      var hasil_rawat_jalan2;

      var xhttp;
      if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
      } else {
        // code for IE6, IE5
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }

      xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
          //document.getElementById("demo").innerHTML = xhttp.responseText; //to print on <p id="demo"></p>
          hasil_rawat_jalan2 = xhttp.responseText;
        }
      }
      
      //this isnt work
      xhttp.open("POST", "<?php echo site_url("
        lab / get_row_content_from_lab_code / ") ?>" + row, true);
      
      //this work
      xhttp.open("POST", "<?php echo site_url("
        lab / get_row_content_from_lab_code /HL-003") ?>", true);

      xhttp.send();

      var number_of_row = parseInt(Object.size(JSON.parse(hasil_rawat_jalan2))); //try to change hasil_rawat_jalan2 to json but fail.
      var row_start = parseInt(row);

      addNewRow(number_of_row);
      var row_end = (number_of_row + row_start);

      j = 1;
      for (i = row_start; i <= row_end; i++) {
        document.getElementById('SUBKLP[' + i + ']').value = hasil_rawat_jalan2[j]['sub_klp'];
        document.getElementById('NAMA[' + i + ']').value = hasil_rawat_jalan2[j]['name_of_inspection'];
        document.getElementById('KODE[' + i + ']').value = hasil_rawat_jalan2[j]['inspection_id'];
        document.getElementById('HASIL[' + i + ']').value = hasil_rawat_jalan2[j]['result'];
        //document.getElementById('NILAI_NORMAL[' + i + ']').value = hasil_rawat_jalan2[j]['normal_result'];
        document.getElementById('NILAI_NORMAL[' + i + ']').value = 'null';
        document.getElementById('SATUAN[' + i + ']').value = hasil_rawat_jalan2[j]['measure_unit'];
        document.getElementById('KDKLP[' + i + ']').value = hasil_rawat_jalan2[j]['klp_id'];
        j++;
      }
      console.log("row start: " + row_start + ", row end:" + row_end + ", column length: " + number_of_row);
    }

problems after update ajax,

  1. I assign var row = "HL-003"; but i wasnt able to assign variable row to xhttp.open("POST", "<?php echo site_url(" lab / get_row_content_from_lab_code / ") ?>" + row, true); unless i write it directly like this xhttp.open("POST", "<?php echo site_url(" lab / get_row_content_from_lab_code /HL-003") ?>", true);
  2. I get result from xhttp.open("POST", "<?php echo site_url(" lab / get_row_content_from_lab_code /HL-003") ?>", true); but it return string not object eventhough format of the string is the same. so i change hasil_rawat_jalan2 = xhttp.responseText; and add JSON.parse(hasil_rawat_jalan2); //try to change hasil_rawat_jalan2 to object but fail.
stacheldraht27
  • 392
  • 1
  • 6
  • 26
  • Does that file run through the php parser? If it's just plain js delivered to the client the php code won't be executed. – m02ph3u5 Oct 15 '15 at 11:00

1 Answers1

1

Its impossible to place a javascript value (clientside) in PHP (serverside). Unless you use AJAX to get the page and send the variable along as a POST/GET value.

javascript values get stored in your browser after the page has been loaded. And PHP executes before the page is sent to the browser. Therefor this is impossible in the way you want it in your question. (For as far as i understand the question :p)

Example for ajax request

Thaillie
  • 1,362
  • 3
  • 17
  • 31