如何使用会话变量创建php图像

时间:2022-09-20 11:08:44

I have a php file called bargraph.php which is creating a bar chart image. I am importing bargraph.php in a seprate file graph.php using:

我有一个名为bargraph.php的php文件,它创建了一个条形图图像。我使用以下命令在一个单独的文件graph.php中导入bargraph.php:

<img src='bargraph1.php'> 

But I need to create bar chart using session variables. I need to pass variable year's value to generate image in bargraph.php. but it is not displaying the graph. I even tried with ajax. It is not happening .

但我需要使用会话变量创建条形图。我需要传递变量year的值来生成bargraph.php中的图像。但它没有显示图表。我甚至试过用ajax。它没有发生。

graph.php

graph.php

<script>
    alert(<?php echo $year ?>);
    var ids = $year;

    if (ids == "") { 
        document.getElementById("graph").innerHTML="";
        return;
    } 
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    var data = "year=" + ids ;
    //var year ="year=" +id1;
    xmlhttp.open("POST", "bargraph1.php", true); 
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
    xmlhttp.send(data);

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("graph").innerHTML=xmlhttp.responseText;
        }
    }
</script>

and

bargraph1.php

bargraph1.php

I am using following code :

我使用以下代码:

$year   = $_POST['year'];
$sql    = "SELECT dt,dist,cal FROM   demo where YEAR(dt)='$year'"; 
$result = mysql_query($sql,$con);

1 个解决方案

#1


0  

As default response from AJAX request is plain text, i'm not sure you can display an image with : document.getElementById("graph").innerHTML=xmlhttp.responseText;

由于AJAX请求的默认响应是纯文本,我不确定您是否可以使用以下内容显示图像:document.getElementById(“graph”)。innerHTML = xmlhttp.responseText;

I suggest another solution writing the image on disk and retrieve image from JS : In your bargraph1.php

我建议另一种解决方案将图像写在磁盘上并从JS中检索图像:在你的bargraph1.php中

$img = /* Image creation*/
$filename = "images/generatedFilename.png";
imagepng($img, $filename);
echo $filename;

You can test your PHP code independently to be sure it works before try using it from JS.

您可以独立测试您的PHP代码,以确保它在尝试使用JS之前可以正常工作。

In your ajax request

在你的ajax请求中

  if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var img = new Image();
            img.src = xmlhttp.responseText;
           document.getElementById("graph").appendChild(img);
        }

EDIT : The part to retrieve year isn't valid in JS.

编辑:检索年份的部分在JS中无效。

alert(<?php echo $year ?>);
var ids = <?php echo $year; ?>;

#1


0  

As default response from AJAX request is plain text, i'm not sure you can display an image with : document.getElementById("graph").innerHTML=xmlhttp.responseText;

由于AJAX请求的默认响应是纯文本,我不确定您是否可以使用以下内容显示图像:document.getElementById(“graph”)。innerHTML = xmlhttp.responseText;

I suggest another solution writing the image on disk and retrieve image from JS : In your bargraph1.php

我建议另一种解决方案将图像写在磁盘上并从JS中检索图像:在你的bargraph1.php中

$img = /* Image creation*/
$filename = "images/generatedFilename.png";
imagepng($img, $filename);
echo $filename;

You can test your PHP code independently to be sure it works before try using it from JS.

您可以独立测试您的PHP代码,以确保它在尝试使用JS之前可以正常工作。

In your ajax request

在你的ajax请求中

  if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var img = new Image();
            img.src = xmlhttp.responseText;
           document.getElementById("graph").appendChild(img);
        }

EDIT : The part to retrieve year isn't valid in JS.

编辑:检索年份的部分在JS中无效。

alert(<?php echo $year ?>);
var ids = <?php echo $year; ?>;