n.am n.am - 2 years ago 56
Javascript Question

how can I use a variable from my php part of the code in the script part?

Here is the php part:

include realpath($_SERVER['DOCUMENT_ROOT'] . '/Classes/Controllers/ReportController.php');
global $scheduleResult = ReportController::getVacRep();

$count = 0;

foreach ($scheduleResult as $rowResult) {
$resultData[count] = $rowResult[vaccValue];

I need to use the $result Data array in this code block, Given that this part of the code is javascript between
<script> </script>

var barChartData = {
labels : ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"],
datasets : [
highlightFill: "#45668e",
highlightStroke: "#45668e",
fillColor : "#1ABC9C",
strokeColor : "#1ABC9C",
data : [90,90,90,90,90,90,90]

I need to use the array in this part in the script code
data : [90,90,90,90,90,90,90]
where in the place of each number here (the 90's) I need to put the values stored in the array but I am not sure how I can use the array from the php in the script code. I tried to google it but I did not understand how to do it or if it is possible.

Answer Source

If your javascript is within the php file, just put <?php echo $data; ?> where you need the data, else you can put a <script> tag in your php page, (the HTML) before your external script tag, and define a global javascript variable in there, so that you can use the data in the external script file.

data : [
  <?php echo $resultData[0]; ?>,
  <?php echo $resultData[1]; ?>,
  <?php echo $resultData[2]; ?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download