George Baca
George Baca

Reputation: 179

PHP to Javascript passing variable returns null results

Here is the code that I have:

https://jsfiddle.net/a6kukf3n/

The PHP variable $weekParses contains the following data:

WK 14,WK 15,WK 16,WK 17,WK 18,WK 19,WK 20,WK 21,WK 22,WK 23,WK 24,WK 25,WK 26,WK 27,WK 28,WK 29,WK 30,WK 31,

However, when I add it to the var weekCases on Javascript and try to print it to the console or my charts.js file it returns null, 1, 2 ,3 ,4 etc

What am I doing wrong? How do I send my PHP variable to JS?

Upvotes: 0

Views: 155

Answers (2)

suman das
suman das

Reputation: 367

Please create a new array in php and assign the value to array

$data_week=array();

while ($weeks = mysqli_fetch_row($numRowsQuery))
    {

      $data_week[]=$weeks

    }

After that please try

<script type="text/javascript">  
    //pass php variable to js for open cases pw
    <?php $weekParse = $data_week; ?>
    var weekCases = <?php echo json_encode($weekParse); ?>; 
</script>

Upvotes: 1

Saurabh
Saurabh

Reputation: 784

Prepare PHP variable inside the PHP code and not in script tag so in your PHP code write

$weekParse = json_encode($weeks);

And your script

<script type="text/javascript">  
    //pass php variable to js for open cases pw
    var weekCases = '<?php echo $weekParse; ?>';    
</script>

Hope this helps you.

Upvotes: 0

Related Questions