Javascript& PHP:如何在php标签中应用javascript变量

时间:2015-10-15 10:26:13

标签: javascript php

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

如何用变量kd_klp替换“HL-024”?

如果我以这种方式使用它,我会收到错误

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

错误说解析错误:语法错误,意外'?&gt;'

如果我的问题不清楚,请询问谢谢^^

更新

添加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);
 }

添加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);
    }

更新ajax后的问题,

  1. 我指定了var row = "HL-003";,但我无法将变量行分配给xhttp.open("POST", "<?php echo site_url(" lab / get_row_content_from_lab_code / ") ?>" + row, true);,除非我直接将其写为xhttp.open("POST", "<?php echo site_url(" lab / get_row_content_from_lab_code /HL-003") ?>", true);
  2. 我从xhttp.open("POST", "<?php echo site_url(" lab / get_row_content_from_lab_code /HL-003") ?>", true);得到结果,但它返回的字符串不是对象,尽管字符串的格式是相同的。所以我更改hasil_rawat_jalan2 = xhttp.responseText;并添加JSON.parse(hasil_rawat_jalan2); //try to change hasil_rawat_jalan2 to object but fail.

1 个答案:

答案 0 :(得分:1)

无法在javascript(服务器端)中放置PHP值(clientside)。 除非您使用AJAX获取页面并将变量作为POST / GET值发送。

在页面加载后

javascript值存储在浏览器中。并且PHP页面发送到浏览器之前执行。因此,在您的问题中,这是不可能的。 (据我所知,问题是:p)

Example for ajax request