[英]Send a request to a php page and then get back results using ajax
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. 我有一个脚本,我试图将一些位置信息发送到php页面,执行mysql搜索查询,并返回结果,而无需转到另一个页面。
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. 我的php工作正常,并且我已经将页面重定向到php页面,但是当我尝试使用下面的代码时,我没有得到任何结果。
Javascript code JavaScript代码
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 + "&long=" +
longitude + "&acc=" + accuracy + "&r=" + radius
+ "&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("<b>username: </b>"+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});
<div id="output">this element will be accessed by jquery and this text replaced </div>
Below is the output from my php query, 以下是我的php查询的输出,
$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'=>"$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
Which gives the following results 得到以下结果
[{"location_accuracy":"122000"},[{"username":"bobbyj","distance":"0.484367160806139"}]]
Sam, I have a few suggestions for you. 山姆,我为您提供一些建议。
First, the jQuery library is great and the AJAX module works amazing :) It's great that you are using it! 首先,jQuery库很棒,而AJAX模块的功能也很出色:)使用它真是太好了! No need to mix that old XMLHTTP junk with it (they do basically the same thing).
无需将旧的XMLHTTP垃圾与之混合(它们基本上做相同的事情)。 So get rid of that and replace it with jQuery ajax.
因此,摆脱它,并用jQuery ajax替换它。
Let's start with something really basic: 让我们从一个非常基本的东西开始:
$.ajax({
url: 'find.php',
type: "POST",
data: { lat: lattitude }
}).done(function( msg ) {
alert(msg);
});
Put your other variables in the data: as well. 也将其他变量放入数据中。
On your PHP page, try a simple var_dump($_POST); 在您的PHP页面上,尝试一个简单的var_dump($ _ POST); so you can see what is coming through.
这样您就可以看到正在发生的事情。 The AJAX should make an alert with contents of the PHP page.
AJAX应该使用PHP页面的内容发出警报。
Work your way up from this with your Mysql :) 使用Mysql从此开始:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.