externalize javascript with php function

General discussions related to php

Moderators: macek, egami, gesf

bostongeorge
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Jan 30, 2014 2:30 am

externalize javascript with php function

Postby bostongeorge » Thu Jan 30, 2014 2:49 am

Hi guys. I am new on php,

I am creating a php index with some scripts inside.
I would like to externalize one of this script that contain a php function.
Therefore I created a js file with the php function and I call it from the index with:
<script type='text/javascript' src="scripts/geo.js" ></script>

The problem is that the geo.js does not run the php function.

How do I make this work?

I read about using ajax,jquery,POST method instead of GET since is more safe.... but ...cannot make it work...any suggestion?

bostongeorge
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Jan 30, 2014 2:30 am

Re: externalize javascript with php function

Postby bostongeorge » Fri Feb 07, 2014 3:19 am

Just to make it more clear:

The problem is that if I leave the whole script into the index everything is working fine. (see code below)
The problem start when I externalize the script.

From the index I am calling the script "geo.js"
The script geo.js contains a function that have to get back the data through a php function.

Code exemple:

<script>
google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {

<!-- Data fetched from the mysql database-->
var data = google.visualization.arrayToDataTable(


[ <?php

echo " ['Country','Risk Level'] ";
$con = mysqli_connect("localhost", "root", "", "globalriskratings");
$sql="
SELECT
countries.name AS name,
ratings.rating AS rating
FROM `ratings`,`countries`
WHERE ratings.mID IN (
SELECT MAX(ratings.mID)
FROM ratings
GROUP BY ratings.cID)
AND ratings.cID=countries.cID
ORDER BY name
";
$result=mysqli_query($con,$sql);
for($i=0;$i<113;$i++){
$row = mysqli_fetch_array($result);
echo ",['";
echo $row['name'];
echo "',";
echo (int)$row['rating'];
echo "]";
};
mysqli_close($con);

?>

]
);
var options = {
region: 'world', // Asia
sizeAxis: { minValue: 1, maxValue: 5 },
colorAxis:{values:[1,2,3,4,5]},
colorAxis:{colors:['#006600','#FFFF00','#FFAA00','#FF5500','#FF0000']},
backgroundColor: '#C4DDFF',
legend:'none',
};
var chart = new google.visualization.GeoChart(document.getElementById('global-risk-map'));
chart.draw(data, options);
};
</script>

In order to externalize the script the code should became in the index file:

<script src='scripts/geo.js'></script>

and in the geo.js the javascript with the php function inside.

At this point I am not sure if I have to put just the php function in another file and call it back from the javascript which in turn back the data to the index.

bostongeorge
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Jan 30, 2014 2:30 am

Re: externalize javascript with php function

Postby bostongeorge » Fri Feb 07, 2014 3:55 am

Modified as following:

1 - In Index.php: <script src='scripts/geo.js'></script>

2 - In geo.js the function:
function drawRegionsMap() {

var a = new XMLHttpRequest();
a.open("GET","geo.php");
a.onreadystatechange = function() {
if( a.readyState == 4) {
if( a.status == 200) {
alert("Worked");
}
else alert("HTTP error "+a.status+" "+a.statusText);
}
}
a.send();

var data = google.visualization.arrayToDataTable(['a']);

......

3 - geo.php
<?php

echo " ['Country','Risk Level'] ";
$con = mysqli_connect("localhost", "root", "", "globalriskratings");
$sql="
SELECT
countries.name AS name,
ratings.rating AS rating
FROM `ratings`,`countries`
WHERE ratings.mID IN (
SELECT MAX(ratings.mID)
FROM ratings
GROUP BY ratings.cID)
AND ratings.cID=countries.cID
ORDER BY name
";
$result=mysqli_query($con,$sql);
for($i=0;$i<113;$i++){
$row = mysqli_fetch_array($result);
echo ",['";
echo $row['name'];
echo "',";
echo (int)$row['rating'];
echo "]";
};
mysqli_close($con);

?>



- The function google.visualization.arrayToDataTable
Have to get back 2 parameters calling the javascript which is calling the php function.
The php Function get back the 2 parameters from the database.

Note that if I put all the script with the function in the index everything is working fine.

bostongeorge
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Jan 30, 2014 2:30 am

Re: externalize javascript with php function

Postby bostongeorge » Fri Feb 07, 2014 4:17 am

note that if I put this code in the index.php everything is working fine

<script type='text/javascript'>
google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {

<!-- Data fetched from the mysql database-->
var data = google.visualization.arrayToDataTable(


[ <?php include('geo.php');?>

]

);
var options = {
region: 'world', // Asia
sizeAxis: { minValue: 1, maxValue: 5 },
colorAxis:{values:[1,2,3,4,5]},
colorAxis:{colors:['#006600','#FFFF00','#FFAA00','#FF5500','#FF0000']},
backgroundColor: '#C4DDFF',
legend:'none',
};
var chart = new google.visualization.GeoChart(document.getElementById('global-risk-map'));
chart.draw(data, options);
};
</script>


Return to “PHP General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron