0

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?

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.