views:

139

answers:

3

Hi , I have this php code and the variable is XML data

$strXML = "<chart caption='ADI Chart Test ' xAxisName='Month' yAxisName='Units'"
$strXML.= "showValues='0'formatNumberScale='0' showBorder='1'>";

echo "<td align='right' onClick='drawchart($strXML)' > $totalcost </td> " ;`

this is passed to a javascript function

function drawchart(dataX) {
var chart1 = new FusionCharts("../charts/Pie3D.swf", "chart1Id", "400", "300","1"); 
chart1.setDataXML(dataX);
chart1.render("chart1div");

My problem is that the link is not displayed correctly and more importantly there is no data when it gets to the js function

Could anyone tell me how to send xml data via a js variable please ?

+1  A: 

You should encode the strXML in order to render it as valid HTML. Furthermore, you should enclose it with apostrophes so that it becomes a valid Javascript literal.

echo
     "<td align='right' onClick='drawchart(\"" .
     htmlspecialchars(json_encode($strXML)) . 
     "\")'> $totalcost </td>";
Alexander Gyoshev
Enclosing it with apostrophes isn't enough, you need to backslash-escape any apostrophes (or other backslashes) in the string before enclosing in single quotes. The easiest way to do all that in one go is: `'<td ... onclick="drawchart('.htmlspecialchars(json_encode($strXML)).');">'`.
bobince
A: 

Hi Mick,

What is the end result you are looking for - ignoring the XML/JS portion?

If you are just trying to assign other attributes to the chart, couldn't you just load the data via the setDataURL function:

var url="/path/to/data.php";

var chart1 = new FusionCharts("../charts/Pie3D.swf", "chart1Id", "400", "300","1");

url=escape(url);

chart1.setDataURL(url);

chart1.addParam("WMode", "Transparent");

chart1.render("chart1div");

malonso
A: 

Seems you have missing something in this : echo "<td align='right' onClick='drawchart($strXML)' > $totalcost </td> " ;`

it should be:

echo "<td align='right' onClick='drawchart(\"$strXML\")' > $totalcost </td> " ;`

sudi