My website prints the user location by calling an API. Say I have the following code all in one file index.php
on my website:
<?php
$sIP = $_SERVER['REMOTE_ADDR'];
$sURL = "http://ipinfo.io/" .$sIP . "/json";
$sJSON = file_get_contents($sURL);
$view = (object) array();
$view->JSON = $sJSON;
?>
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<script>
// global string of Data
var gsData = <?= $view->JSON ?>
$(document).ready(function(){
var sRegion = gsData.region;
$('#result').html(sRegion);
});
</script>
<div>Region from data is:</div>
<div id="result"></div>
</body>
Though, I need to keep the PHP script and the markup separated. I can't embed the PHP script in a script
element. What can I do to have a PHP and a HTML file?