KRA KRA - 1 year ago 106
Javascript Question

how to assign php array values to javascript array

Please provide help on how to assign php array values to javascript array

----------- Using below php code I am storing data in php array -----------

$str_query="SELECT title,description FROM tablename ORDER BY title";

$schd_arr = array();
$schd_arr[$int_count] = $rs_sch->Fields("title").": ".$rs_sch->Fields("description");

----- Using below javascript code I am trying to store php array data into javascript array -----

Please let me know what and how to write variable at below 2 mentioned locations so my code can work.

<script type="text/javascript" language="javascript">
var pausecontent=new Array()
for (sch_cnt=0; sch_cnt<*Here I want to assign value of $int_count php variable*; sch_cnt++)
pausecontent[sch_cnt]=<?php *Here I want to assign php array and counter values (something like this - $schd_arr[sch_cnt];)* ?>;

Thank you in advance,

Answer Source

You can't loop like that, you need to loop the PHP array and push into javascript array:

<script type="text/javascript" language="javascript">
    var pausecontent = new Array();
    <?php foreach($schd_arr as $key => $val){ ?>
        pausecontent.push('<?php echo $val; ?>');
    <?php } ?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download