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.

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
user2192094
  • 337
  • 2
  • 6
  • 15
  • possible duplicate of [JavaScript Variable inside string without concatenation - like PHP](http://stackoverflow.com/questions/3304014/javascript-variable-inside-string-without-concatenation-like-php) – Quentin Feb 21 '14 at 16:59

2 Answers2

1

This should do it

xmlhttp.open("GET","getrecord.php?q=" + numlat + "&r=" + numlong, true);
xmlhttp.send();
nettux
  • 5,270
  • 2
  • 23
  • 33
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);
    });
}
whatisdot
  • 26
  • 1