Pass Values from PHP to Javascript
There are many ways to pass PHP variables to Javascript but I will only cover 3 most common: 1. Echo the data. 2. Use a hidden field or element. 3. Use a data attribute 1. Echo the data. The first one which is the simplest can be done as follows: var data = <?php echo $data; ?>; But the above code can cause an issue depending on the data type and it's value, so we use json_encode to convert the data. In PHP: $name = "John O'Reily"; // a string with single quote $age = 14; // an integer number $address = array( // an array 'street' => '123 Street', 'city' => 'XYZ City' ); In Javascript: var name = <?php echo json_encode($name); ?>; var age = parseInt(<?php echo json_encode($age); ?>); var address = <?php echo json_encode($address); ?...