2017-10-07 67 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