简体   繁体   English

如何将可变的javascript传递给php

[英]how to pass variable javascript to php

I need some help, I will create chart using morris.js but i find some problem, when i use detailIp in pipeline mongoDB in php tag (as show bellow) but no result show. 我需要一些帮助,我将使用morris.js创建图表,但是当我在php标签中的管道mongoDB中使用detailIp时出现了一些问题(如下所示),但未显示任何结果。 I dont know why. 我不知道为什么。 How to pass detailIp so it can use in php tag ? 如何传递detailIp以便可以在php标签中使用? thank before and sorry my english is so bad :( Here my code: 在此先感谢,对不起,我的英语太糟糕了:(这是我的代码:

     <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>

you must use ajax : 您必须使用ajax:

<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>

in php file : 在php文件中:

<?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]."},";
    }
?>

You can do an ajax call to pass your js varibale to your php code. 您可以进行ajax调用,以将js varibale传递给您的php代码。 Here is an example. 这是一个例子。

$.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*/
          }
      });

Hope it's helpful 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM