1

So i have this code(JavaScript) and the webpage i am creating is with google maps api. This is the code:

var myLatLng; 
for(var i = 0; i < locationLatLang.length; i++) {
 myLatLng = new google.maps.LatLng({lat: parseFloat(locationLatLang[i][1]), lng: parseFloat(locationLatLang[i][2])});
 if(google.maps.geometry.poly.containsLocation(myLatLng, polyLine[index]) || google.maps.geometry.poly.isLocationOnEdge(myLatLng,  polyLine[index], 0.000000)){
  console.log('True' + ' - ' + myLatLng.lat() + ' = ' + myLatLng.lng());
  <?php
   include 'update_graph.php';
  ?>
 }
}

My question is, whenever i put include 'update_graph.php'; it does not show anything on the page, just a blank page. nothing appears. But when i remove that, it loads the page and shows everything. Is there a way i could get this to work. or is it not possible to require or include a php file in javascript to query a database?

OneFromFew
  • 23
  • 5
  • 1
    something in `update_graph.php` is resulting in output that is not valid javascript - what errors do you get in your browsers developer tools console ... posting the content (or an idea of it) of that file would be super useful in telling you where the error is – Jaromanda X Feb 20 '16 at 07:14
  • It's wrong, you can't use php inside javascript. see the deference between server side and client side languages. – inverted_index Feb 20 '16 at 07:15
  • it does not show any error. and inside the `update_graph.php` is just a piece of code. it does nothing. here it is: `require('../connect.php'); $query = 'SELECT Brgy_Name FROM location WHERE Latitude = '+myLatLng.lat()+' AND Longitude = '+myLatLng.lng+';';` – OneFromFew Feb 20 '16 at 07:20
  • so it's not possible to access a database and query from it using php inserted in javascript? is there any other way i can query from the database, sir? – OneFromFew Feb 20 '16 at 07:21
  • look at the source code of the page **in the browser** ... what is there after your `console.log` - that's the output of `update_graph.php` – Jaromanda X Feb 20 '16 at 07:23
  • okay, it shows html properties. something colspan blah blah blah.. i'll try to finish the `update_graph.php` and see if it changes anything. thank you for the ^ advice @JaromandaX – OneFromFew Feb 20 '16 at 07:33

1 Answers1

3

It is not possible to use the javascript variables in the php code. To do that you should create an Ajax call to a php file and pass those variables to that page. Hope this can help.

Ashish
  • 468
  • 2
  • 8
  • 24