'Accessing Html/php file having Js inside it using ajax
I have one index.php from where i am accessing noun_phrase.php to return a variable. Now I have to pass the returned variable from noun_phrase.php to another php file placeapi.php to execute certain task.
index.php
if(dialog.indexOf("restaurant")!=-1 ){
$.ajaxSetup({async: false});
showText="";
sayText="";
$.ajax({
url: "noun_phrase.php",
data: {text: dialog},
success: function(data) {
nlp = data;
findPlace(nlp);
}
});
}
function findPlace(x){
name = x;
$.ajax({
url: "placeapi.php",
data: {text: name},
success: function (data){
}
});
}
placeapi.php
<!DOCTYPE html>
<html>
<head>
<title>Place searches</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCmrjT6f4nt15qV-L9MRWL43TgucZzJHCw
&libraries=places"></script>
<script>
var map;
var infowindow;
function initialize() {
var city = new google.maps.LatLng(-42.882391,147.328591);
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: city,
zoom: 15
});
var request = {
location: city,
radius: 500,
types: ['hotel']
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas">
</div>
</body>
</html>
I want to pass a data retrieved from noun_phrase.php to javaScript function of placeapi.php and get location so that i can display somewhere
Solution 1:[1]
You simply echo the value inside the constructor google.maps.LatLng
function initialize() {
var city = new google.maps.LatLng(<?php echo $_POST['latitude'] ?>,<?php echo $_POST['latitude'] ?>);
// longitude and latitude are part of `npl`
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: city,
zoom: 15
});
var request = {
location: city,
radius: 500,
types: ['hotel']
};
Ensure your add method:POST in your ajax
Solution 2:[2]
If you want to access through javascript then send a get request to noun_phrase.php.
And echo or return the response from noun_phrase.php
And get this response from placeapi.php
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Friday Ameh |
| Solution 2 | mosafir |
