Okay, I guess I need a conceptual help here. I am not sure if it is the right forum to ask but it's more like a php issue than pure javascript.
Here is the scenario:
1. I have a search box on home page
2. when users click the search button, a javascript function is triggered
3. on homepage I have an empty div that I'd like to populate with the search results
4. the javascript function calls php (assynchronous calls)
5. php returns some data (in xml format)
6. the javascript parse the xml data
7. the javascript function assign that data into the empty div
8. User sees search result on home page.
I've managed to do all of these. Now I need to know -
1. How can I display the search results on search_result.php page and not on home page. i.e How do I tell javascript to load that search result into a particular page?
(if anyone is interested, I am using google map API to get the search result, and is then gets displayed with the data itself and matching map. i.e map plotting)
Thanks for your help.
php/js - how to load returned data in a different page?
Moderator: General Moderators
- mecha_godzilla
- Forum Contributor
- Posts: 375
- Joined: Wed Apr 14, 2010 4:45 pm
- Location: UK
Re: php/js - how to load returned data in a different page?
To do this you'd need to generate a URL for each result with some GET values appended to it, something like:
This then becomes the input for your search script (essentially the search gets run twice.) I assume that's what you're trying to do anyway, otherwise if you want the search results to only appear on the search_result.php page then you just need to get rid of all the AJAX stuff on the home page so it's basically just a form (remember to do server side validation though.) You might want to create another script that makes the homepage work more like Google's search pages (IE try and identify what the user is searching for while they're typing it) but I guess that's a project for another day.
HTH,
Mecha Godzilla
Code: Select all
<a href="search_result.php?search_term=New%20York">New York</a>
HTH,
Mecha Godzilla
Re: php/js - how to load returned data in a different page?
I didn't fully comprehend the method you describe. Let me show you what I've done with my code. Maybe you can then point me to the area where I can implement your idea.
This is home.php. User sees teh big search box the the top of the page (this search box is visible from all other pages as well)
This is the search box:
In the body tag I load google map
Now the javascript part:
Finally time to display that result :
Like I said in my previous post, I've managed to do all of it.
With my current solution it works only if I have the divs declared on the page. For example, If I am on the home page and use the search it works because on my home page I've the divs declared and hence results can be displayed. On the other hand,If I try to search from say, contact us page, it wont show me anything as contact us page doesnt have these divs declared. So, javascript doesn't know where to load those sidebar and map data.
And having to declare these divs in each page is awkward. Ideally, I'd rather want to take the search term from any page, process it and display it on a certain page - for example, search_result.php.
I'd assume some redirection would be required but not sure about it. Any idea how can I achive this?
P.S: That php page that is being called from javascript returns an xml file.
This is home.php. User sees teh big search box the the top of the page (this search box is visible from all other pages as well)
This is the search box:
Code: Select all
<input type="text" id="addressInput"/>
<input type="button" onclick="searchLocationsNear()" value="Search Locations"/>
Code: Select all
<body onload="load()" onunload="GUnload()">
Code: Select all
<script type="text/javascript">
//<![CDATA[
var map;
var geocoder;
function load() {
if (GBrowserIsCompatible()) {
geocoder = new GClientGeocoder();
map = new GMap2(document.getElementById('map'));
map.setCenter(new GLatLng(51, -1.87), 10);
}
}
function searchLocationsNear(center) {
// returns the name of the stores
var searchUrl = 'phpsqlsearch_genxml.php?postcode=' + postcode;
GDownloadUrl(searchUrl, function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName('marker');
map.clearOverlays();
var sidebar = document.getElementById('sidebar');
sidebar.innerHTML = '';
if (markers.length == 0) {
sidebar.innerHTML = 'No results found.';
map.setCenter(new GLatLng(40, -100), 4);
return;
}
var bounds = new GLatLngBounds();
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute('name');
var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
parseFloat(markers[i].getAttribute('lng')));
var marker = createMarker(point, name);
map.addOverlay(marker);
var sidebarEntry = createSidebarEntry(marker, name);
sidebar.appendChild(sidebarEntry);
bounds.extend(point);
}
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
});
}
function createMarker(point, name) {
//var marker = new GMarker(point);
var marker = new GMarker(point, customIcons[approved]);
var html = name;
GEvent.addListener(marker, 'click', function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
function createSidebarEntry(marker, name) {
var div = document.createElement('div');
var html = name;
div.innerHTML = html;
return div;
}
//]]>
</script>
Code: Select all
<div id="sidebar"></div>
<div id="map" width:400px; height:400px"></div>
With my current solution it works only if I have the divs declared on the page. For example, If I am on the home page and use the search it works because on my home page I've the divs declared and hence results can be displayed. On the other hand,If I try to search from say, contact us page, it wont show me anything as contact us page doesnt have these divs declared. So, javascript doesn't know where to load those sidebar and map data.
And having to declare these divs in each page is awkward. Ideally, I'd rather want to take the search term from any page, process it and display it on a certain page - for example, search_result.php.
I'd assume some redirection would be required but not sure about it. Any idea how can I achive this?
P.S: That php page that is being called from javascript returns an xml file.
- mecha_godzilla
- Forum Contributor
- Posts: 375
- Joined: Wed Apr 14, 2010 4:45 pm
- Location: UK
Re: php/js - how to load returned data in a different page?
Ok - I see what you mean now 
The problem you've got here is that by doing everything in JavaScript (which is essentially stateless) you can't really take results generated in one page and apply them to another one. A simpler approach (for you) would be to have one 'master' page into which any content can be displayed (either by turning different <div> layers on and off as necessary or just loading each content item in dynamically using AJAX.) An alternative would be to move your JavaScript so that it only works on the search results page, and you use the search form on every other page to send a value to the PHP script that generates the search page, then the PHP script will need to load a value into your JavaScript (IE the page template loads first, then you call your JavaScript function to run the search.)
Hopefully that makes things a bit clearer.
M_G
The problem you've got here is that by doing everything in JavaScript (which is essentially stateless) you can't really take results generated in one page and apply them to another one. A simpler approach (for you) would be to have one 'master' page into which any content can be displayed (either by turning different <div> layers on and off as necessary or just loading each content item in dynamically using AJAX.) An alternative would be to move your JavaScript so that it only works on the search results page, and you use the search form on every other page to send a value to the PHP script that generates the search page, then the PHP script will need to load a value into your JavaScript (IE the page template loads first, then you call your JavaScript function to run the search.)
Hopefully that makes things a bit clearer.
M_G
Re: php/js - how to load returned data in a different page?
by value you mean the search term?and you use the search form on every other page to send a value to the PHP script
Can you give me an example using my code above?
that generates the search page, then the PHP script will need to load a value into your JavaScript
- mecha_godzilla
- Forum Contributor
- Posts: 375
- Joined: Wed Apr 14, 2010 4:45 pm
- Location: UK
Re: php/js - how to load returned data in a different page?
Yes, for "value" read "search term". The search form on each page is just used to pass the search term to your PHP script, which then just passes the search term to your JavaScript script.
Depending on how you structured the page (and presuming you're using PHP to generate all the page tags) you could put the search term in the <body> tag, something like:
For that to work you'd need to make sure that the relevant JavaScript has already been loaded by the time the page calls the 'onload' method IE put it between the <head></head> tags I would have thought. If that doesn't work as expected then you might need to call some of your JavaScript functions from within the body of the page rather than the header.
Alternatively, if you just append the search term to the URL then you should be able to access it from within JavaScript anyway - this site shows how to do this:
http://www.netlobo.com/url_query_string_javascript.html
Make sure you follow the link to the comments page as there are a few other suggestions that might work better for you.
HTH,
M_G
Depending on how you structured the page (and presuming you're using PHP to generate all the page tags) you could put the search term in the <body> tag, something like:
Code: Select all
<body onload="load('search_term_goes_here')" onunload="GUnload()">Alternatively, if you just append the search term to the URL then you should be able to access it from within JavaScript anyway - this site shows how to do this:
http://www.netlobo.com/url_query_string_javascript.html
Make sure you follow the link to the comments page as there are a few other suggestions that might work better for you.
HTH,
M_G