How to send php value to javascript with script tag?

  html, javascript, php

php code is

                                            <?php
                                            if (isset($_POST['search']))
                                            {
                                                $startDate = $_POST['start'];
                                                $startDate = str_replace('/', '-', $startDate ); 
                                                $startDate = date("Y-m-d", strtotime($startDate));
                                                // echo $startDate;
                                                
                                                $endDate = $_POST['end'];
                                                $endDate = str_replace('/', '-', $endDate ); 
                                                $endDate = date("Y-m-d", strtotime($endDate));
                                                // echo $endDate;

                                                $model = $_POST['model'];
                                                // echo $model;

                                                $dates = getDatesStartToLast($startDate, $endDate);


                                                // echo $dates
                                                for ($i=0; $i < count($dates); $i++){
                                                    echo "<form method = 'post'>";
                                                    echo "<tr>";
                                                    echo "<td><button type = 'submit' style = 'border-color : white; background-color : white; outline : 0; border : 0' name = 'datebutton' value = '$model,$dates[$i]'>$dates[$i]</td>";
                                                    echo "</form>";
                                                    $query = mysqli_query($conn, "SELECT * from elentec_count where Date = '$dates[$i]' and model = '$model'");
                                                    $row = mysqli_fetch_row($query);
                                                    echo "<td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td><td>$row[6]</td><td>$row[7]</td>";
                                                }    
                                                $burrCordResult = updateChart($conn, $model, $dates);
                                                $burrCordResult = ['mon', 'Tue'];
                                                print_r($burrCordResult);
                                                
                                            }
                                            
                                            
                                            ?>
<script>var BurrLoc = "<?= $burrCordResult ?>";</script>

javascript code is

console.log(BurrLoc)

The php code is in the middle of the html code. It makes the error
Uncaught ReferenceError: BurrLoc is not defined

I don’t know why this matter comes out.

Source: Ask PHP

LEAVE A COMMENT