I read How to pass variables and data from PHP to JavaScript? and http://www.dyn-web.com/tutorials/php-js/json/multidim-arrays.phpbut I can't pull it together...pretty overwhelming and I'm just a beginner (they even say to use AJAX...wow!). Could you help we guyz ?
My situation is :
- I have on php script stored in htdocs xampp apache server ;
- I have a html / javascript set of code in another folder (it's a cordova app)
I want to call my php script from the onload event of the body.
Idealy I'd like to do :
function init(){
// some instructions
var myvar;
myvar = result of execution of my_php_script;
//using myvar in some other instructions
}
You may see below in the last lines of my JS how I intend to use this "code" decribed above
My Current HTML
<body onload="init();">
<div id="map">
</div>
</body>
My current JS
function init(){
var mymap = L.map('mapid').setView([51.505, -0.09], 13);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18,
id: 'your.mapbox.project.id',
accessToken: 'your.mapbox.public.access.token'
}).addTo(mymap);
}
var myvar = result of execution of php script (a table)
var marker = L.marker([myvar[0][1], myvar[0][2]]).addTo(mymap);
}
my current php (this php script works fine right now. I wanna return $row in the javascript var myvar)
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
try {
$hostname = "SQLSERVER_INSTANCE_1";
$port = 1433;
$dbname = "MY_BD";
$username = "user";
$pw = "pass";
$dbh = new PDO ("sqlsrv:Server=$hostname,$port;Database=$dbname","$username","$pw");
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
$sth = $dbh->prepare("SELECT * FROM dbo.COORD");
$sth->execute();
/*while($row = $sth->fetch(PDO::FETCH_ASSOC)){
print $row['lat']. "<br/>";
}*/
$row = $sth->fetch(PDO::FETCH_ASSOC);
echo $row['lat'];
?>