Bootstrap Graph not rendered while using a dynamic query

时间:2017-06-15 09:51:59

标签: php ajax graph

The graph executes correctly when i use a static query. But when i use a dynamic query it doesnt render even when the query returned is same to the static query.

//$sql = "SELECT `". $_GET["field2"]  ."`, COUNT(*) as defectCount FROM `defects` GROUP BY `". $_GET["field2"] ."`";
            $sql = "SELECT `user_13`, COUNT(*) as defectCount FROM `defects` GROUP BY `user_13`"; 

Here the commented code doesnot works but the other one works though they throw the same query when i checked on my console.

//code in pie.php

$("#submitgraph").click(function()
    {
        var graph_type=$("#graph_type").val();

        var field_one=$("#field_type1").val();
        var field_two=$("#field_type2").val();

        $.ajax({
        url: "http://localhost/ac/data/data.php?field1="+field_one+"&field2="+field_two, 
        type: "GET", 
        async: true,
        success: function(response){
            console.log(response);


        setTimeout(function(){ 
            if(graph_type=="piechart")
        {
            document.getElementById("pie_draw").style.display = "block";
        }
            }, 1000);


        }
        });




});

////code in data.php

      $servername = "localhost";
       $username = "root";
        $password = "";
        $dbname = "hp";


        $conn = new mysqli($servername, $username, $password, $dbname);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 

            $sql = "SELECT `". $_GET["field2"]  ."`, COUNT(*) as defectCount FROM `defects` GROUP BY `". $_GET["field2"] ."`";
//the commented code works       
 //$sql = "SELECT `user_13`, COUNT(*) as defectCount FROM `defects` GROUP BY `user_13`";
        $result = $conn->query($sql);

        $data = array();
                        foreach ($result as $row) {
                        $data[] = $row;
                        }

                    $result->close();
                    $conn->close();
                    echo json_encode($data);


        ?>

//code in data_a.js

    //Flot Pie Chart
    $(function() {
     //var data ;
    $.ajax({
            url : "data/data.php",

            type : "GET",
            dataType: 'json',
            success : function(data){

            var datas = [];            
            for(i=0;i<data.length;i++) { 
            var obj = {
                 label : data[i].user_13,
                 data : data[i].defectCount
            };
                 datas.push(obj);                   
            }   









    var plotObj = $.plot($("#flot-pie-chart"), datas, {
        series: {
            pie: {
                show: true
            }
        },
        grid: {
            hoverable: true
        },
        tooltip: true,
        tooltipOpts: {
            content: "%p.0%, %s", // show percentages, rounding to 2 decimal places
            shifts: {
                x: 20,
                y: 0
            },
            defaultTheme: false
        }
    });

                  }
    });


});

1 个答案:

答案 0 :(得分:0)

I Think one of the problem is that your ajax request type is

type : "POST"

in code in pie.php

but in your data.php your request type become GET

Try to change the

$_GET["field2"]

to

$_POST["field2"]

let me know if it works.