简体   繁体   English

将请求发送到php页面,然后使用ajax返回结果

[英]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.

相关问题 使用ajax将发布请求发送到php页面 - Send a post request to a php page using ajax 如何使用jquery ajax将异步请求发送到php页面 - how to send asynchronous request to php page using jquery ajax Ajax 向当前页面发送 post 请求 php - Ajax send post request to the current page php 我无法从使用Javascript(Ajax)的PHP得到答复。 虽然我可以在PHP页面上显示结果,但我希望将其重新显示在HTML页面上 - I can't get a reply from PHP using Javascript (Ajax). While I am able to display results on PHP page, I want it back on the HTML page 发送一个AJAX请求以处理数据并获取多个PHP响应以更新其进度 - Send One AJAX Request to process data and Get Back Multiple PHP Responses updating its progress 通过ajax方法GET将Google标记管理器结果发送到php页面 - send google tag manager results to php page via ajax method GET 使用按钮单击以将结果返回到PHP - Using a button click to get results back in PHP 我如何使用jquery ajax在同一个文件中发送多个请求并在php中获取所需的响应 - How do i send multiple requests for the same file and get the required response back in php using jquery ajax 在 php 中发送 GET 请求而不加载页面 - send GET request without loading page in php 使用AJAX将数据发送到.php文件,如何从中获取数据? - Using AJAX to send data to a .php file, how do I get data back from it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM