How can I pass an array string to JavaScript functions from the end of PHP as an argument?

I get a missing error message ) after the argument in my Firebug console.

emissing) after the argument http://a8.sphotos.ak.fbcdn.net/hphotos-ak-snc7/s720x720/393131_320846714645076_100001592501599_911297_470580896_n.jpg

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

Define php variable:

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

      

Call JavaScript function with define argument

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

      

JavaScript function

<script>
function dynamicChartArray(div,chartdata){
var myData = new Array(chartdata);
var myChart = new JSChart(div, 'pie');
alert(chartdata+div);
}
<script>

      

+3


source to share


7 replies


Instead of creating an array from a string in javascript, why not just make PHP output it as an array to start with?

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

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

      

then strip the quotes in the output (which are responsible for the error messages appearing)



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

      

and then myData can just map the chart data (since it is already an array)

var myData = chartdata;

      

+2


source


Instead

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

      

Using

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

      



Change your call to this:

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

      

And follow these steps:

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

      

+2


source


'<?php echo $chartdata;?>'

      

It will echo '['NBA',1],['NFL',2],['MLB',3],['NHL',4]'

. Note that there are single quotes in single quotes.

 new Array(chartdata)

      

This will just make an array with one element, a string "['NBA',1],['NFL',2],['MLB',3],['NHL',4]"

.

Try to do dynamicChartArray('<?php echo $div;?>',[<?php echo $chartdata;?>])

This will make an chartdata

array of arrays.

+2


source


change this:

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

      

:

dynamicChartArray('<?php echo $div;?>', [<?php echo $chart_data;?>]);

      

and see if it works

+1


source


You don't need to var myData = new Array(chartdata);

.

chartdata

is already an array.

+1


source


Take a look at json_encode .

$chart_data = json_encode(array(array('NBA',1),array('NFL',2)));

      

which will create a json string ready to echo into your script

string(21) "[["NBA",1],["NFL",2]]"

      

+1


source


You have to look at the result. I am sure that it is:

dynamicChartArray('graph','['NBA',1],['NFL',2],['MLB',3],['NHL',4]')

      

and you can already see that you have quotes problems.

Instead of creating a string, I suggest creating an array and using json_encode

:

$chart_data = array(
    array('NBA',1),
    array('NFL',2),
    array('MLB',3),
    array('NHL',4)
);

      

and

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

      

JSON is also valid JavaScript and it provides more options for server side data processing.

+1


source







All Articles