Including external JavaScript in PHP file and calling a function - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Friday, June 4, 2021

Including external JavaScript in PHP file and calling a function

I have a PHP file in which I have a <script> tag where I defined chartIt() function to draw a chart.js graph. I need to use chartIt() function in another file so I want to move whole code from <script> tag to external JavaScript file but when I do that i got an error. My take:

<script type="text/javascript" src="myjavascript.js"></script>

if(!empty($_POST['btn'])){ 
        switch($_POST['btn']){
            case '1':
                ?>
    <script>
    chartIt(x_amp, 'X axis', "Red");
    </script>
    <?php
                break;

Errors I got:

Uncaught SyntaxError: Unexpected token '<'

index.php:121 Uncaught ReferenceError: chartIt is not defined at index.php:121



source https://stackoverflow.com/questions/67827376/including-external-javascript-in-php-file-and-calling-a-function

No comments:

Post a Comment