在PHP中访问JavaScript变量

时间:2017-03-22 06:51:39

标签: javascript php mysql ajax

我在PHP代码中设置AJAX变量时遇到问题

当我更改区域的值时,将调用以下函数:

function find_map(){

    var value1 = $("#area").val();
    var value2 = $("#city").val();

        $.ajax({
        url :"find_my_map.php", // json datasource
        type: "post",  // method  , by default get
        dataType:"json",
        data:
        {
            area_id:value1,
            city_id:value2
        },
        success: function(data) 
          {

            console.log(data);
            $.each(data, function(index, element) {

                alert("area_name:" + element.area_name);
                alert("city_name:" + element.city_name);

                 var map_area_name= element.area_name;

                var map_city_name= element.city_name;

                 $("#map_area_name").val(map_area_name);
                 $("#map_city_name").val(map_city_name);


            });
          },
          error:function(data){
                console.log(data);
          }

    });


}

find_my_map.php文件

include_once("config.php");
if(isset($_POST['area_id']) && isset($_POST['city_id']))
{
    $area_id = $_POST['area_id'];
    $city_id = $_POST['city_id'];


        $sql = "SELECT * FROM area a, city c WHERE a.city_id=c.city_id AND c.city_id='$city_id' AND a.area_id='$area_id'";
        $qry = mysql_query($sql);
        while($fetch = mysql_fetch_array($qry))
        {

            $area_name=$fetch['area_name'];
            $city_name=$fetch['city_name'];


            $data[]=array( 

            'area_name' => $area_name,
            'city_name' => $city_name



            );  
        }


    $json_data = array($data);
    echo json_encode($data);
}

这是我的PHP代码

<?php
  echo $address ="here i want to use **map_area_name AND map_city_name**"; // Google HQ

  $prepAddr = str_replace(' ','+',$address);
  $geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
  $output= json_decode($geocode);
  $area_latitude = $output->results[0]->geometry->location->lat;

  $area_longitude = $output->results[0]->geometry->location->lng;

  echo "<br><input type='text' name='area_latitude' id='area_latitude'value='".$area_latitude."'> <br>";

  echo "<input type='text' name='area_longitude' id='area_longitude' value='".$area_longitude."'>";
?>

0 个答案:

没有答案