将PHP结果与PHP集成的最佳方法 [英] Best way to Integrate a Javascript result with PHP

查看:87
本文介绍了将PHP结果与PHP集成的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



基本上我使用Google Maps API - 它是Javascript。



我的网站我的意图是调用Google Maps API来获取各种已经经过地理编码的经度和纬度点之间的距离。然后我想把它们显示给我的用户。



一个例子。



我有(在php中):

  $ home = array(latitude,longitude); 

我的位置数据库也有一个mysql_query结果。


$ ($ result = mysql_fetch_array($ results)){

$ b

  
//输出GOOGLE MAPS JAVASCRIPT JARGON,它将找到距离
//在我的家和数据库位置之间

}

在示例代码中,通过将指定div的内部html设置为某个查询的响应值,将值简单地输出到div在那里我的JS知识变得朦胧)。

理想情况下,我想把这个值变成一个php变量。



有没有做:

  $ var = js thing的响应; 

目前我所做的主要是使用php我已经生成了正确数量的div 'response1','response2'等,这些都是JS填充的,但这似乎不是最合理的做事方式。



希望这是有道理的。
谢谢

解决方案

从JavaScript发送答案至PHP需要AJAX:

如果您可以使用几乎是标准的JQuery,那非常简单。这是我写给我的PHPPro课程的一个经验:

receiveNumbers.php

 <?php 
if(isset($ _ GET ['sentNums']))
{
$ sentNumbers = filter_input(INPUT_GET,'sentNums',FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
//转换为数组。
$ numbers = split(',',$ sentNumbers);
//回声。
echo json_encode($ numbers)。
}

index.php

 < html xmlns =http://www.w3.org/1999/xhtmlxml:lang =enlang =en> 
< head>
< title>图表排序| PHPExperts.pro< /标题>
< script type =text / javascriptsrc =js / jquery-1.4.2.min.js>< / script>
< script type =text / javascriptsrc =js / jquery.json-2.2.min.js>< / script>
< script type =text / javascript>
var myNumbers ='5,10,22,11';
var receivedNumbers;

<?php
//从JavaScript发送数据 - > PHP。
if(!isset($ _ GET ['myData']))
{
?>
jQuery.getJSON('receiveNumbers.php?sentNums ='+ myNumbers,function(jsonReceived)
{
receivedNumbers = jQuery.parseJSON(jsonReceived);
alert(receivedNumbers); //预计[5,10,22,11]
});
<?php
}
?>
< / script>
< / head>
< / html>


Ok.

Basically I am utilizng the Google Maps API - It is Javascript.

My site runs off PHP for the most part.

My intention is to make calls to the Google Maps API to get the distance between various already geocoded longitude and latitude points. I then want to display them to my users.

An example.

I have (in php):

$home=array(latitude,longitude); 

I also have a mysql_query result from my database of locations.

As such I do

while($result=mysql_fetch_array($results)){

//OUTPUT THE GOOGLE MAPS JAVASCRIPT JARGON WHICH WILL FIND THE DISTANCE
//BETWEEN MY HOME AND THE DB LOCATIONS

}

In the example code the value is simply outputted to a div by setting the inner html of a specified div to the response value of a certain query (this is where my JS knowledge gets hazy).

Ideally I want to get this value into a php var.

Is there anyway of doing:

$var=response from js thing;

What I have done at the moment is essentially using php I have generated the correct number of divs with names 'response1','response2' etc which the JS populates.. but this does not seem like the most logical way of doing things.

Hope that makes sense. Thanks

解决方案

To send an answer from JavaScript to PHP requires AJAX:

If you can use JQuery, which is pretty much a standard, it's really easy. This is verbatim from one of the lessons I wrote for my PHPPro course:

receiveNumbers.php

<?php
if (isset($_GET['sentNums']))
{
    $sentNumbers = filter_input(INPUT_GET, 'sentNums', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    // Convert to array.
    $numbers = split(', ', $sentNumbers);
    // Echo out.
    echo json_encode($numbers).
}

index.php

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head>
        <title>Graph Sort | PHPExperts.pro</title>
        <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="js/jquery.json-2.2.min.js"></script>
        <script type="text/javascript">
var myNumbers = '5, 10, 22, 11';
var receivedNumbers;

<?php
// Send data from JavaScript -> PHP.
if (!isset($_GET['myData']))
{
?>
    jQuery.getJSON('receiveNumbers.php?sentNums=' + myNumbers, function(jsonReceived)
    {
        receivedNumbers = jQuery.parseJSON(jsonReceived);
        alert(receivedNumbers); // Expected [5,10,22,11]
    });
<?php
}
?>
       </script>
   </head>
</html>

这篇关于将PHP结果与PHP集成的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆