Calling a php file within a html file using javascript

Aiden Doyle

I have a PHP file that outputs my google analytics tracking code from my database. But unfortunately my website front end is HTML. Is there a way using Javascript to call my file templates/google-analytics-code.php and output my google tracking code?

This is my PHP file that outputs my tracking code:

<?php
include_once"../database.php";

$qry="select * from google_analytics";
$result=mysqli_query($con,$qry) or die(mysqli_error($con));

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $google_analytics_id = $row["google_analytics_id"]; 
    }
}

echo $google_analytics_id;
?>

index.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="google-site-verification" content="VFDwnGJ-4 
    RPz2jHchu3ARhbY2GLqkvyII4IbtAR-aP0" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://www.istreamradio.ie/css/bootstrap.css">
    <link rel="stylesheet" href="http://www.istreamradio.ie/css/font-awesome.css">
    <meta name="google-signin-client_id" content="80957862538-juiu2cgia32rn3lik36fv9a1ihc6fqof.apps.googleusercontent.com">
    <link rel="stylesheet" href="http://www.istreamradio.ie/css/animate.css">
    <link rel="stylesheet" href="http://www.istreamradio.ie/css/style.css">
    <link rel="stylesheet" href="http://www.istreamradio.ie/css/player.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <title>Listen Radio</title>
    <base href="/">
</head>

<body ng-app="musiclistener">
    <p><span ng-include="'templates/cookiebar-settings.php'"></span>
        <span ng-cloak>
        <span ng-view ></span>
        </span>
    </p>
    <div id='player-container'>
        <audio controls id='music-player' src="#"></audio>

        <div class='container-fluid'>
            <div class='col-sm-1 col-xs-3 text-center' id='play-icon-container'>
                <i class='fa fa-play' id='play-btn' ng-click="playtoogle()">
        </i>
            </div>
            <div class='col-sm-1 center-block hidden-xs' id='podcast-icon-container'>
                <img src="{{ playerthumb }}" id='play-img' class='img-responsive center-block'>
            </div>
            <div class='col-md-6 col-sm-4 col-xs-6' id='podcast-bar-container'>
                <span style='color:#fff;position:relative;top:3px;text-transform:capitalize' ng-if="musicplayingentity">{{ musicplayingentity}}</span>
                <div id='podcast-progress'>
                    <div id='podcast-id-value'></div>
                </div>
            </div>
            <div class='col-md-1 col-sm-2 text-center hidden-xs' id='addons-icon-container'>

                <span ng-hide="userLoggedIn">
                    <a href data-toggle="modal" data-target="#loginModal"><i 
    class='fa fa-plus pull-left'></i></a>           
                    <a href data-toggle="modal" data-target="#loginModal"><i 
    class='fa fa-comment '></i></a>
                </span>

                <span ng-show="userLoggedIn">
                    <i ng-click="makeFavoritePlayer()" ng-hide="playingfav" class='fa fa-plus extrafun'></i>
                    <i ng-click="removeFavoritePlayer()" ng-show="playingfav" class='fa fa-check extrafun'></i>
                    <i ng-click="showCommentBoxPlayer()" class='fa fa-comment '></i>
                </span>

                <i class='fa fa-share pull-right' ng-click="shareboxPlayer()"></i>
            </div>
            <div class='col-sm-3 col-xs-3 text-center' id='volume-container'>
                <div class='col-xs-2'>
                    <i class='fa fa-volume-up'></i>
                </div>
                <div class='col-xs-1 col-md-9'>
                    <div id='volume-progress'>
                        <div id='volume-id-value'></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://apis.google.com/js/platform.js" async defer></script>
    <script src="http://www.istreamradio.ie/ang/angular.min.js"></script>
    <script src="http://www.istreamradio.ie/ang/angular-sanitize.min.js"></script>
    <script src="http://www.istreamradio.ie/ang/angular-route.min.js"></script>
    <script src="http://www.istreamradio.ie/ang/angular-cookies.min.js"></script>
    <script src="http://www.istreamradio.ie/ang/angular-facebook-sdk.js"></script>
    <script>
        window.fbAsyncInit = function () {
            FB.init({ appId: '195962897544265', xfbml: true, version: 'v2.8' });
        };
    </script>
    <script src="http://www.istreamradio.ie/ang/app.js"></script>
    <script src="http://www.istreamradio.ie/ang/controllers.js"></script>
    <script src='http://www.istreamradio.ie/js/jquery.js'></script>
    <script src='http://www.istreamradio.ie/js/bootstrap.js'></script>
    <script src='http://www.istreamradio.ie/js/typed.js'></script>
    <script src='http://www.istreamradio.ie/js/wow.js'></script>
    <script src='http://www.istreamradio.ie/js/player.js'></script>
    <script>
        new WOW().init();
    </script>
    <script>
        $(function () {
            $(".typing-text").typed({
                strings: ["MUSIC", "SPORTS", "BOOKS", "NEWS", "TALK"],
                typeSpeed: 200,
                backSpeed: 150,
                loop: true
            });
        });
    </script>
</body>
</html>
Chava Geldzahler

You can do that with AJAX.

<script type="text/javascript">    
$.ajax({
    type: "GET",
    url: 'templates/google-analytics-code.php',  // make sure to give this a valid path
    success: function(data){
        // the data returned is passed to the success function.  You can do anything with it in here, like appending it to the DOM with jQuery.
        $('body').append(data);
    }
});
</script>

Note: You will need to have jQuery loaded for this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling Python file from within PHP

From Dev

Calling PHP functions from within the same file

From Dev

Using PHP file via JavaScript/Jquery using an HTML file only

From Dev

Issue with calling a javascript file from a HTML page

From Dev

Calling .js file from HTML using AJAX

From Dev

Javascript: calling function within D3 from another file

From Dev

Calling a php function in a separate file using AJAX

From Dev

Calling a Javascript function from an external js file in the html file

From Dev

Calling php file from javascript using jQuery.get(), location of php?

From Dev

PHP function and calling it within html

From Dev

Calling method in PHP file from separate JavaScript file

From Dev

Uploading a file using HTML PHP

From Dev

Trying to post a javascript variable to a php file using the ajax method : POST but getting an undefined index in $POST array within php file

From Dev

Using HTML file type in JavaScript

From Dev

Using dynamic html in javascript file

From PHP

AJAX not calling PHP file

From Dev

how to include javascript between html tags all within a javascript file

From Dev

Using PHP variable inside HTML, in a PHP file

From Dev

Using a PHP session variable within a MySQL file run in PHP?

From Dev

Inserting Data into MySQL without calling PHP file inside HTML

From Dev

Calling JIRA REST Api from Javascript in local HTML file

From Dev

Calling Javascript function from external file when loading html page

From Dev

Is it possible to target a html element within a .aspx file from within the corresponding .cs file using ASP.NET?

From Dev

Calling Javascript function defined in external file through PHP

From Dev

how to redirect to a php file within an html select tag

From Dev

Using php file() function to put a html or php file into array

From Dev

WebStorm: How to prettify HTML in quotes within a JavaScript file

From Dev

HTML Help Workshop - Is it possible to use CSS or JavaScript within a .CHM file?

From Dev

tagging a javascript file within html script tag successfully

Related Related

  1. 1

    Calling Python file from within PHP

  2. 2

    Calling PHP functions from within the same file

  3. 3

    Using PHP file via JavaScript/Jquery using an HTML file only

  4. 4

    Issue with calling a javascript file from a HTML page

  5. 5

    Calling .js file from HTML using AJAX

  6. 6

    Javascript: calling function within D3 from another file

  7. 7

    Calling a php function in a separate file using AJAX

  8. 8

    Calling a Javascript function from an external js file in the html file

  9. 9

    Calling php file from javascript using jQuery.get(), location of php?

  10. 10

    PHP function and calling it within html

  11. 11

    Calling method in PHP file from separate JavaScript file

  12. 12

    Uploading a file using HTML PHP

  13. 13

    Trying to post a javascript variable to a php file using the ajax method : POST but getting an undefined index in $POST array within php file

  14. 14

    Using HTML file type in JavaScript

  15. 15

    Using dynamic html in javascript file

  16. 16

    AJAX not calling PHP file

  17. 17

    how to include javascript between html tags all within a javascript file

  18. 18

    Using PHP variable inside HTML, in a PHP file

  19. 19

    Using a PHP session variable within a MySQL file run in PHP?

  20. 20

    Inserting Data into MySQL without calling PHP file inside HTML

  21. 21

    Calling JIRA REST Api from Javascript in local HTML file

  22. 22

    Calling Javascript function from external file when loading html page

  23. 23

    Is it possible to target a html element within a .aspx file from within the corresponding .cs file using ASP.NET?

  24. 24

    Calling Javascript function defined in external file through PHP

  25. 25

    how to redirect to a php file within an html select tag

  26. 26

    Using php file() function to put a html or php file into array

  27. 27

    WebStorm: How to prettify HTML in quotes within a JavaScript file

  28. 28

    HTML Help Workshop - Is it possible to use CSS or JavaScript within a .CHM file?

  29. 29

    tagging a javascript file within html script tag successfully

HotTag

Archive