1

I am currently trying to use AJAX to retrieve selected records from DB. I have 2 files - browser.php and getrecord.php.

In browser.php, I use geolocation javascript to get latitude and longitude and store them in global variables:

browser.php

var numlat;//store latitude
var numlong;//store longitude

function loadrecord(numlat,numlong)
{
  if(numlat=="" || numlong==""){
    document.getElementById("box").innerHTML="";
    return;
  }

  if (window.XMLHttpRequest){
    //code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
  }
  else{
    //code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

  xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readystate==4 && xmlhttp.status==200){
      document.getElementById("box").innerHTML=xmlhttp.responseText;
    }
  }

  xmlhttp.open("GET","getrecord.php?q=numlat&r=numlong", true);
  xmlhttp.send();
}

My main question is how do I sent a request to the server for getrecord.php using numlat and numlong as the 2 parameters? I will need to use them var numlat and var numlong values to do a SELECT in my database and display them in browser.php.

getrecord.php

$q = $_GET['q'];//i am trying to get the latitude 
$r = $_GET['r'];//i am trying to get the longitude

//i will need to use $q and $r to do a SELECT in my database.

Sorry, if this is a noob question.

1

2 Answers 2

1

It can be valuable to know the details of how XMLHttpRequest objects work in JavaScript, but it can be very cumbersome to do this all the time.

I would recommend using a JavaScript framework like jQuery. It would shorten your code to this:

function loadrecord(numlat,numlong)
{
    if(numlat=="" || numlong==""){
        $("#box").html("");
        return;
    }

    $.get("getrecord.php?q="+numlat+"&r="+numlong, function(response_text) {
        $("#box").html(response_text);
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

This should do it

xmlhttp.open("GET","getrecord.php?q=" + numlat + "&r=" + numlong, true);
xmlhttp.send();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.