ammaps / amcharts using dataLoader and passing post

1.7k Views Asked by At

Using ammaps' dataLoader, is there a way to pass post parameters to an external file?

I'm creating a report that contains a map, which when clicked, will provide a list of news articles from a MySQL table. I'm pulling in json data using dataLoader. However, I need to be able to pass a value (reportId) to the script that generates the json file.

here's my code:

var map = AmCharts.makeChart("mapdiv", {
  type: "map",
  "dataLoader": {
    "url": "maparticlesfeed.json.php",
    "format":"json",
    "showErrors": false//,
    // tried this:  
    //"type" : "post",
    //"data" : {"reportIdFromPost" : 1}
  },
. . . //the rest of the map config 

And this is maparticlesfeed.json.php:

<?php include('database-connect.php'); 
$con=Database::connect();
$query=$con->prepare("SELECT * from articles Where reportId=$POST['reportIdFromPost']");
$query=>execute();
$articlesList=$query->fetchall(PDO::FETCH_ASSOC);
?>

{
"map" : "MyMapName",
"_comment" : "Here, instead of hard-coding the headlines, we will iterate through $articlesList",
"areas" : [   
    { "title" : "Virginia",
        "id" : "US-VA", 
        "selectable" : true,
        "numArticles" : 4,
        "articles" : [
            { "headline" : "This is the first headline",
                "link" : "link url"
            },
            { "headline" : "This is the second headline",
                "link" : "link url"
            },
            { "headline" : "This is the third headline",
                "link" : "link url"
            },
            { "headline" : "This is the fourth headline",
                "link" : "link url"
            }
        ]
    },
    { "title" : "Tennessee",
        "id" : "US-TN", 
        "selectable" : false,
        "numArticles" : 6
    } 
  ]
} 
3

There are 3 best solutions below

1
Alex Russell On BEST ANSWER

I went the jQuery route to solve this. I'd still like to know if there's a way to do this with dataLoader, but here's a workaround in case anyone else has this issue in the future:

$.ajax({
  dataType: "json",
  url: "maparticlesfeed.json.php",
  data: { reportIdFromPost: 1 },
  type: "post",
  success: function(data){ 
    console.log(data); // for debugging
    makeMap(data);
  },
  error: function(data) {
    console.log('it did not work.');
    console.log(data.responseText); 
  }
});
function makeMap(theData) {
    var map = AmCharts.makeChart("mapdiv", {
    type: "map",
    /* DON'T NEED THIS ANYMORE:  
      "dataLoader": {
      "url": "maparticlesfeed.json.php",
      "format":"json",
      "showErrors": false
    },*/
    // replace instead with this:
    "dataProvider" : theData,
    // ... the rest of the map config ...
    }
});

And, in maparticlesfeed.json.php:

<?php 
include('database-connect.php'); 
$con=Database::connect();

if(!empty($_POST) && isset($_POST['reportIdFromPost']) ) { 
    $postedReportId= $_POST['reportIdFromPost']; 
}

include('database-connect.php'); 
$con=Database::connect();
$query=$con->prepare("SELECT * from articles Where reportId=$postedReportId ");
$query->execute();
$articlesList=$query->fetchall(PDO::FETCH_ASSOC);
?>

{
"map" : "MyMapName",
"areas" : [
       // Code to json_encode the results from $articlesList
    ]   
}
1
phpchap On

It may be that you're posting json data and then not decoding it when you're trying to use the value in your php script.

Try this at the top of maparticlesfeed.json.php:

<?php
include('database-connect.php');

$postedValues = json_decode($POST['reportIdFromPost'], true);

$con=Database::connect();
$query=$con->prepare("SELECT * from articles Where reportId=$postedValues['reportIdFromPost']");
$query=>execute();
$articlesList=$query->fetchall(PDO::FETCH_ASSOC);
?>

You'll also need to uncomment the two lines in the map config:

"type" : "post",
"data" : {"reportIdFromPost" : 1}
0
Alberto Dousdebes On

Maybe you could add the parameters in the "url" parameter of the dataloader.

"url": "urlA.do?param1=" + $('.selector').val()

and then in some trigger (ie. button click) modify the url and reload

var url = "same URL with different parameters values";
chart.dataLoader.url = url;
chart.dataLoader.loadData()