0

I know, I know, one is client side and one is server side. I had to use PHP to grab a few arrays of data and I'm passing this data in to Google Charts geomaps. E.g:

echo 'var data = google.visualization.arrayToDataTable([
              ["Country", "Popularity"],
      ';
      foreach($countries as $key=>$val){
           echo '["'.$key.'",'.$val.'],';
      }
echo ']);

When a user clicks on one of the countries (using an event listener) on the map it puts this value into a variable:

var country = data.getValue(selectedItem.row, 0);

Now I need to pass this to the PHP array, so I can do something like:

if($country == country){
//do whatever
}
4
  • You need to have <script></script> tags when using JavaScript, however I doubt that will fix it. Commented Oct 17, 2015 at 11:30
  • 3
    use ajax call to send data to php file and you can return some data as well if you want Commented Oct 17, 2015 at 11:30
  • 1
    First of all: you need to learn to distinguish between JS running within your browser and PHP running on your webserver. Then you will see, that there's no way of "passing a variable from JS to PHP" in one source like $country == country. Instead, make yourself familiar with AJAX and JSON. Google for these terms and find that intro suitable for you. Commented Oct 17, 2015 at 11:37
  • What is 'do whatever' is important. If it's just a few calculations and other basic stuff, code it in javscript. If it's a db-query, you have to send an ajax request, as others mentioned. Commented Oct 17, 2015 at 11:43

1 Answer 1

1

To send a Javascript value to PHP you'd need to use AJAX. With jQuery, it would look something like this (most basic example possible):

 var country = data.getValue(selectedItem.row, 0);
 $.post('file.php', {country: country});
Sign up to request clarification or add additional context in comments.

1 Comment

This seems like a great idea, but I can't quite get it to work. When I send the country over, and save to $_SESSION, it can only be recalled back on page refresh. I can't really use function(data) for it either because that's a JS variable...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.