SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Location
    West Midlands, UK
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using JS and PHP together?

    I have been using JS and PHP for a while but have never had to use them together.

    I am developing a google map application. The user can drag a pointer around the map and when they land on a hot spot on the map, for example they land on a local park, information will be sent from the database into the info window.

    For those who are not familiar with Google Maps I am basically sending a request to a database and loading the information into the Javascript on the current page.

    I understand this has something to do with AJAX.

    My question is, how do I load the information I get back from the PHP page into the Javascript?

    Actually, come to think of it, I am not even sure how to send the information from Javascript to the PHP file to query the database with the current coordinates.

    I am guessing you have an even handler so when the pointer is dropped (dragend) it will send a request to the PHP file to query the database with the current coordinates. If no corrdinates match in the database the user is not on a hotspot on the map.

    Again, to summarize, I need to know how to send information from JS (such as coordinates) to a PHP file. I also need to know how to load information gathered by a PHP file into Javascript.

    Thanks.

  2. #2
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to create an XML object to access the other site, something like this

    Code:
    // javascript
    // URL is the url where you want to access the data
    // callback_func is the function to call once you have reterived the data
    // post_vars are any variables you want to send to the URL
    // resp_xml, hmmm I cant remember what that is... my bad need to check some of my old code
    function send_http_request(url , callback_func , post_vars , resp_xml) {
      var xmlobj = null;
      try {
        xmlobj = new XMLHttpRequest();
      }
    
      catch(e) {
        try {
          xmlobj = new ActiveXObject("Microsoft.XMLHTTP");
        }
    
        catch(e) {
          alert("AJAX isn\'t supported by your browser!");
          return false;
        }
      }
    
      xmlobj.onreadystatechange = function() {
        if(xmlobj.readyState == 4) {
          if(xmlobj.status == 200) {
            resp_xml ? eval(callback_func + "(xmlobj.responseXML)") : eval(callback_func + "(xmlobj.responseText)");
          }
        }
      }
    
      xmlobj.open("POST" , url , true);
      xmlobj.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded; charset=UTF-8");
      xmlobj.send(post_vars);
    }
    
    // usage
    send_http_request('google.com' , 'what_to_do_with_google_data' , 'location_im_looking_for=durban');
    Thats the basic function you need to do the request (and sample how to use it)

    You would have to have some kind of event (probably onclick) on your map, and as the person clicks call send_http_request() with the coordinates, and the rest is up to your code

    Happy coding, hope this is what you were looking for


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •