Note that there are some explanatory texts on larger screens.

plurals
  1. POSend a request to a php page and then get back results using ajax
    text
    copied!<p>I have a script where i am trying to send some location information to a php page, carry out a mysql search query and get the results back without going to another page.</p> <p>my php works fine, and i have had the page working that it redirects to the php page, however when i try and use the code below, i do not get any results passed back.</p> <p>Javascript code</p> <pre><code> function phpRedirect(loc) { // var radius = get('r'); // Retrieve GET values for search radius and // var numResults = get('n'); // number of results var radius = 10; // Retrieve GET values for search radius and var numResults = 5; // number of results var latitude = loc.coords.latitude; // Get long, lat and accuracy from var longitude = loc.coords.longitude; // location object var accuracy = loc.coords.accuracy; var xmlHttp = new XMLHttpRequest(); //not the cross browser way of doing it xmlHttp.open("GET", "find.php?lat=" + latitude + "&amp;long=" + longitude + "&amp;acc=" + accuracy + "&amp;r=" + radius + "&amp;n=" + numResults, true); xmlHttp.send(null); } $(function () { $.ajax({ url: 'find.php', //the script to call to get data type: "post", data: { getData: true }, dataType: 'json', //data format success: function(data) //on recieve of reply { var name = data[0]; $('#output').html("&lt;b&gt;username: &lt;/b&gt;"+username); } }); }); function error(loc) { // This is called if the location can't be found. document.write("Error finding GPS location"); } // Use navigator to get current user location. If found, calls 'phpRedirect()', // If not available calls 'error()'. Includes timeout and ensures highest acc. navigator.geolocation.getCurrentPosition(phpRedirect, error, {maximumAge:60000, timeout:5000, enableHighAccuracy:true}); &lt;div id="output"&gt;this element will be accessed by jquery and this text replaced &lt;/div&gt; </code></pre> <p>Below is the output from my php query, </p> <pre><code> $result=mysql_query($query) or die (mysql_error()); while($row=mysql_fetch_assoc($result)) $data[]=$row; // Turn result to array $acc_package = array('location_accuracy'=&gt;"$accuracy"); // Include result array $output[] = $acc_package; // and accuracy value inside $output[] = $data; // an output array. print(json_encode($output)); // Convert output array to json format and print </code></pre> <p>Which gives the following results</p> <pre><code> [{"location_accuracy":"122000"},[{"username":"bobbyj","distance":"0.484367160806139"}]] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload