Query Master Query Master - 2 years ago 102
PHP Question

how to pass array string in JavaScript function from PHP end as a argument?

I am getting the error missing ) after argument list in my Firebug console.

emissing ) after argument

My question is how to pass $char_data variable in JavaScript function as a argument

Define php variable:

$chart_data = "['NBA',1],['NFL',2],['MLB',3],['NHL',4]";
$div = "graph";

Call JavaScript function with define argument

dynamicChartArray('<?php echo $div;?>','<?php echo $chartdata;?>')

A function of JavaScript

function dynamicChartArray(div,chartdata){
var myData = new Array(chartdata);
var myChart = new JSChart(div, 'pie');

Answer Source

Rather than creating an array out of a string in javascript, why not just just get the PHP to output it as an array to start with?

Just add an extra set of [] which javascript reads as an array.

$chart_data = "[['NBA',1],['NFL',2],['MLB',3],['NHL',4]]"; 

then ditch the quotes on the output (which are responsible for causing the error messages)

 dynamicChartArray('<?php echo $div;?>', <?php echo $chartdata;?>);

and then myData can just equal chart data (since its already an array)

var myData = chartdata;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download