Jump to content

gmap help with ajax


friedice

Recommended Posts

hello, i got my gmaps working couple months ago using PEAR templates and php with js

however i was thinking of changing it using ajax to make it more beta

right now:

searchdoctor.php -> forms for users to select criteria, posts all that data into

resultdoctor.php and then calls a query into the database to search and return a map using js with displaymap.php

 

wat im lookin after for and tryin to do is  to instead of redirecting the page , it can use ajax to load the result page into the search one under the forms or something..

 

is this possible?

thx :)

 

Link to comment
Share on other sites

Yes, it is very possible and not difficult.  Create a DIV under your form that is blank and has an ID.  Then use an ajax call to load displaymap into the target DIV.  I assume you will have to pass your form data through the ajax call.  That is also not difficult.  Assume your div is set up like this:

<div id="mapbox"></div>

And your form has 2 fields and a submit:

<input type="text" id="street">
<input type="text" id="city">
<input type="button" value="Submit" onclick="javascript:showmap();">

 

In the <HEAD> section of your page, you'll need something like this:

<SCRIPT>
//Define AJAX call
function createXMLHttpRequest() {
if (typeof XMLHttpRequest != 'undefined') { 
	return new XMLHttpRequest(); 
} 
try { 
	return new ActiveXObject("Msxml2.XMLHTTP"); 
} catch (e) {
		try { 
			return new ActiveXObject("Microsoft.XMLHTTP"); 
		} catch (e) {}
}
return false; 
}

//Define function to call your php file and send it to it's target
function showmap() {
var xmlHttp_out = createXMLHttpRequest();
var street= document.getElementById("street").value;
var city= document.getElementById("city").value;

params = "street=" + street+ "&city=" + city;
xmlHttp_out.open("POST","displaymap.php", true);
xmlHttp_out.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

xmlHttp_out.onreadystatechange = function() {
	if(xmlHttp_out.readyState == 4 && xmlHttp_out.status == 200) {
		document.getElementById("mapbox").innerHTML = xmlHttp_out.responseText;			
	}
}
xmlHttp_out.send(params);
}
</SCRIPT>

 

On pressing the submit button, it calls the Javascript function showmap().  The function reads the contents of your fields and passes them via the POST method to displaymap.php and displays the results in the "mapbox" div.  Every time the submit button is pushed it will repeat with whatever new data is in the fields.

 

Link to comment
Share on other sites

ahk cool thx for that tip

but i have several displaymap.php like 3 and all do abit different things on the map

in those displaymap.php, all that is inside are all js code deguised in php to hide the js from the user, so everything that the search page posts all go to the result page which makes the query to the database to display the details and then calls the js map in display map..

 

could i just call

xmlHttp_out.open("POST","result.php", true);

this page from the search and then calls the map one?

Link to comment
Share on other sites

I'm not sure I follow what you are trying to do there.  If you are trying to open a php file within a php file from an ajax call, I'm not sure that will work.  Remember that xmlHttp_out is simply a variable assigned to a return from the function that creates the XML request.  What you CAN do is include the 1st file as a function that return the data for the 2nd file and call that via ajax.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.