AJAX:将图表数据显示为IMG

时间:2011-10-09 00:26:07

标签: php ajax charts image

我想使用AJAX显示(图表)图像。我不确定出了什么问题,但是我收到了以下“错误”和错误的图片: “|xtt I {飨BBBN : } ̛7oРA7n 0l߾}QQQIIIeee aLHH m wm v U & Z o #Art]]] { #“” ' v| ҥKqqq ح 〜;11 # ޺ ںm6O 7o . ~~! ~ 7 O 0A. cv TäR) ˗{N 5< &0 o @ NХ<0 j 0 = ] t 7 j T*5 \ 3g F GFMݻ'的.....“

我正在使用的代码:

ajax_select.php:

<html>
<head>
<script type="text/javascript">

var xmlhttp;

function showUser(str,age)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="test_ajax.php";
url=url+"?q="+str+"&a="+age;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML="<IMG SRC='" + xmlhttp.responseText + "'/>";
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

</script>
</head>
<body>

<form>
<select id="users" name="users">
<option value="">Select a person:</option>
<?php

$con = mysql_connect(***);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("***", $con);

$sql="SELECT ***";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
  {

  }

mysql_close($con);
?>
</select>

<select id="age" name="age">
<option value="">Select a person:</option>
<?php

$con = mysql_connect('***');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("***", $con);

$sql="SELECT ***";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
  {
  }

mysql_close($con);

?>
</select>

<input type='button' value='Refresh Data' onclick="showUser(document.getElementById('users').value,document.getElementById('age').value)">
</form>

<br><br>
<div id="txtHint"><b>chart will be displayed here.</b></div>

</body>
</html> 

ajax_select_NEW.php:

<script type="text/javascript">

var xmlhttp;

function showUser(str,age)
{
var url = 'test_ajax.php';
url += '?q=' + str + '&a=' + age + '&sid=' + Math.random();

document.getElementById('txtHint').innerHTML = '<img src="' + url + '" />';

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }

xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}


function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

</script>

test_ajax.php:

<?php
/* Include the pData class */
include("class/pData.class.php");
include("class/pDraw.class.php");
include("class/pImage.class.php");

/* Get user from AJAX resquest */
$user_id=$_GET["q"];

$q=$_GET["q"];
$a=$_GET["a"];

/* Create the pData object */
$MyData = new pData();  

/* Connect to the MySQL database */
$db = mysql_connect("***");
mysql_select_db("***",$db);




/* Build the query that will returns the data to graph */

$Requete = "

SELECT ***
";

***
/* Render the picture (choose the best way) */
$myPicture->autoOutput("examples/example.drawBarChart.png");

?> 

我现在已经对SQL代码中的变量进行了硬编码。 (在test_ajax.php中)因此,如果我打开该页面,它只显示正确的图表图像。但是当我打开ajax_select.php页面时,我得到上图中的错误。 (所以这不是错误的图表代码信息,因为如果我直接打开php页面就可以了)

我搜索了很多,但找不到解决方案。希望有人能在这里帮助我,我将不胜感激!

1 个答案:

答案 0 :(得分:3)

您正尝试将二进制图像数据放入src的{​​{1}}属性中。此属性适用于图片的源网址,您可以在没有img的情况下完全执行此操作,只需使用XmlHttpRequest作为test_ajax.php插入图片。

src

对于图像的损坏渲染,您是否包含function showUser(str, age) { var url = 'test_ajax.php'; url += '?q=' + str + '&a=' + age + '&sid=' + Math.random(); document.getElementById('txtHint').innerHTML = '<img src="' + url + '" />'; } - 标题?

Content-Type

这是 ajax_select_NEW.php 应该是这样的:

header('Content-Type: image/png');
$myPicture->autoOutput("examples/example.drawBarChart.png");