0

I'm making a map using leaflet.js right now I'm getting countries through a json file using php as data provider and sending the ajax request to get data

PHP that provides File:

 <?php
    header("Access-Control-Allow-Origin: *");
    ini_set('display_errors', 'On');
    error_reporting(E_ALL);

    $data=file_get_contents("capitals.geojson");
    $data= json_decode($data, true);
    $data2= $data["features"];

    echo json_encode($data2);
?>

Ajax Request that is getting data from php file:

$.ajax({
        url: "php/capitals.php",
        type: 'GET',
        dataType: 'json',
        success: function(result) {
        countryList=result;
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log("error");
    }
});

Right now I'm choosing a specific object using this code:

option.value="GB";
const selectedCountry = countryList.find( countryObj => countryObj.id === option.value);
console.log(selectedCountry);

Instead of getting specific object from this code I want to get it from the PHP routine so that when an ajax request is passed the file returns only the specific object that I need.

I tried getting the specific object from json file using javascript but I want to get it through PHP.

brombeer
  • 8,716
  • 5
  • 21
  • 27
  • Hey can you log the json file too so that I can have a better look at json structure? – Muhammad Usman Oct 26 '22 at 07:03
  • First you're going to need to pass the value of `option.value` to PHP in the AJAX request. Then you need probably need to read https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value – ADyson Oct 26 '22 at 07:59

0 Answers0