3

I have problem to set the AJAX variable in use in my PHP code

When I change the value of area the following function will be called:

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 file

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);
}

this is my php code

<?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."'>";
?>
9
  • any error you are getting in your browser console panel + on php side? Commented Mar 22, 2017 at 6:53
  • 1
    First of all dont use those mysql_ functions anymore. They are deprecated since years and are not supported anymore in PHP7. Beside that, there are security issues. Commented Mar 22, 2017 at 6:53
  • Try to edit this line $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.";" Commented Mar 22, 2017 at 6:55
  • stackoverflow.com/questions/2379224/… Commented Mar 22, 2017 at 7:01
  • stackoverflow.com/questions/2338942/… Commented Mar 22, 2017 at 7:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.