2017-10-07 197 views
1

我需要一些幫助,我將創建使用圖表morris.js但我發現一些問題,當我在MongoDB的管道在PHP代碼中使用detailIp(如顯示波紋管),但沒有結果表明。我不知道爲什麼。 如何通過detailIp這樣它可以在php標籤中使用? 感謝之前和遺憾,我的英語是如此糟糕:( 這裏我的代碼:如何通過javascript變量到PHP

 <script> 

        $(document).on("click", ".open-detail", function() { 

         var detailIP = $(this).data('id'); 
         document.getElementById("header_ip").innerHTML = "IP "+detailIP; 
         document.getElementById('ip').innerHTML =detailIP; 


          Morris.Area({ 
          element: 'graph1', 
          behaveLikeLine: true, 
          data: [ 

          <?php 
          ini_set('max_execution_time', 600); 
          ini_set('memory_limit', '2048M'); 
          $m = new MongoClient("192.168.80.20"); 
          $db= $m->blacklist; 
          $col=$db->score; 
          $cursor=$col->find(array("ip_blacklist"=>"'".detailIP."'")); 
          $detail_ip=array(); 
          foreach ($cursor as $document) { 
          $detail_ip[]=$document; 
          } 

          $score=array(); 
          $time=array(); 
          for ($a=0; $a < sizeof($detail_ip); $a++) { 
          $score[]=$detail_ip[$a]["score"]; 
          $time[]=date("Y-m-d H:i:s",$detail_ip[$a]["timestamp"]->sec); 
          } 
          for ($i=0; $i < sizeof($detail_ip) ; $i++) { 
           echo "{date:'".$time[$i]."',count:".$score[$i]."},"; 
          } 
         ?> 
          ], 
          xkey: 'date', 
          ykeys: ['count'], 
          labels: ['Y'] 
         }); 
        if($('#graph1').find('svg').length > 1){ 
         $('#graph1 svg:first').remove(); 
         $(".morris-hover:last").remove(); 
           } 
        }); 

</script> 
+1

閱讀上如何Ajax的工作原理。 php只能在服務器上運行,而不能在瀏覽器上運行 – charlietfl

+0

不要將'php'代碼與'js'混合使用 - 這是一種不好的做法。您需要通過ajax發送'detailIP',然後從您的後端檢索數據或從後端獲取數據(仍然是ajax),並在javascript中對其進行修改。 –

回答

0

你可以做一個ajax調用JS的傳遞varibale你的PHP代碼。 下面是一個例子。

$.ajax({ 
      url: "/your_controller_the_php_code/the_action_the_php_function", 
      dataType: "json", 
      data: {       
       your_js_variable: detailIP     
      }, 
      success: function(data) { 
       /*this is where you do what you want to do when you get the response from your php code*/ 
      } 
     }); 

希望它有助於

+0

感謝您的幫助,是的,我使用Ajax和我解決這個問題 – user7059470

0

必須使用AJAX:在PHP文件

<script> 

    $(document).on("click", ".open-detail", function() { 

     var detailIP = $(this).data('id'); 
     document.getElementById("header_ip").innerHTML = "IP "+detailIP; 
     document.getElementById('ip').innerHTML =detailIP; 

    $.ajax({ 
     type: "get", 
     url: "... php file address ...", 
     format : 'html', 
     success: function (data) 
     { 


       Morris.Area({ 
       element: 'graph1', 
       behaveLikeLine: true, 
       data: [data], // <<=== data from ajax 
       xkey: 'date', 
       ykeys: ['count'], 
       labels: ['Y'] 
      }); 
      if($('#graph1').find('svg').length > 1){ 
       $('#graph1 svg:first').remove(); 
       $(".morris-hover:last").remove(); 
         } 
      }); 

     } 
    }); 



</script> 

<?php 
    ini_set('max_execution_time', 600); 
    ini_set('memory_limit', '2048M'); 
    $m = new MongoClient("192.168.80.20"); 
    $db= $m->blacklist; 
    $col=$db->score; 
    $cursor=$col->find(array("ip_blacklist"=>"'".detailIP."'")); 
    $detail_ip=array(); 
    foreach ($cursor as $document) { 
    $detail_ip[]=$document; 
    } 

    $score=array(); 
    $time=array(); 
    for ($a=0; $a < sizeof($detail_ip); $a++) { 
    $score[]=$detail_ip[$a]["score"]; 
    $time[]=date("Y-m-d H:i:s",$detail_ip[$a]["timestamp"]->sec); 
    } 
    for ($i=0; $i < sizeof($detail_ip) ; $i++) { 
     echo "{date:'".$time[$i]."',count:".$score[$i]."},"; 
    } 
?> 
+0

感謝幫助下,終於解決了,是的,我需要的AJAX和PHP文件需要json_encode和腳本無需數據類型:「JSON」 – user7059470