开发者

AJAX: Display Chart data as IMG

I want to show a (chart) image using AJAX. I'm not sure what's wrong, but I'm getting the following 'error' and an incorrect image: "|xtt�I������{饗BBBN�:��}�̛7oРA7n�0l߾} QQQIIIeee�aLHH������ضm��wm���v��U�&�Z���o�# Art]]]����{���#""��'���v|�ҥKqqq���ح�~;11�ȑ#����޺u��ںm6O�7o���.��ի��?~Ȑ!��~��۷��O�0A.�cv�����TäR)�� ����˗{N����5<��&0� ���ҷo��@�NХ<0�j�0��=���]�t��۷�j�T*5�\����۳g�F�����gfm���ݻ�'OF....."

The code I'm using:

ajax_select.php:

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

var xmlhttp;

function showUser(str,age)
{
xmlhttp=GetXmlHttpObject();
if (xm开发者_如何学运维lhttp==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");

?> 

I have hard coded the variables in the SQL code for now. (in test_ajax.php) So if I open that page it just shows the correct chart image. But when I open the ajax_select.php page I get the error in the picture above. (so it's not a wrong chart code information, since it's okay if I open the php page directly)

I have searched a lot, but can't find the solution. Hopefully someone can help me here, would be much appreciated!


You're trying to put the binary image data into the src attribute of the img. This attribute is meant for the source URL of the image, you can do this entirely without XmlHttpRequest, just insert your image by using test_ajax.php as the 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 + '" />';
}

As for the broken rendering of the image, have you included a Content-Type-header?

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

This is what ajax_select_NEW.php should look like:

<script type="text/javascript">
function showUser(str, age) {
    var url = 'test_ajax.php';
    url += '?q=' + str + '&a=' + age + '&sid=' + Math.random();

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜