Global variable?

I’m able to retrieve data from the database but I’m having trouble using the returned variable. At the bottom of the code example I inserted DISPLAY JAVASCRIPT VAR region here??? where I need to use the region variable. How can I change my code to do that? Here’s my code:


     function handlePosition(pos)
{ $.ajax({                                      
      url: 'map.php',
      data: "lat=" + pos.coords.latitude + "&lon=" + pos.coords.longitude,
      dataType: 'json',
      success: function(data)
      { region = data[0]; } 
    });
} 
      </script>
      <select id="cities" multiple="multiple" name="cities[]" title="Confirm Your Deal Location"  multiple="multiple" data-native-menu="false">
      <?php $sql = "SELECT DISTINCT name, id
FROM `db` WHERE id != '1' ORDER BY name
";
  $sql_result = mysql_query($sql) or die(sprintf("Couldn't execute query, %s: %s", db_errno(), db_error()));
while ($row = mysql_fetch_array($sql_result)) {
 $mailregion = $row['name'];
 $group_id = $row['id']; ?>
      <option value="<?php echo "$group_id"; ?>" <?php if (($mailregion == 'Internet') || ($mailregion == DISPLAY JAVASCRIPT VAR region here???)) { echo "selected=\\"selected\\""; } ?>><?php echo "$mailregion"; ?></option>

Thanks in advance,

Tim

[list][]PHP runs on the server-side
[
]The web page is created and sent to the client
[]The client browser interprets the page
[
]JavaScript then runs on the client[/list]

It is not possible for JavaScript to be run at the same time as PHP, so PHP cannot directly get any JavaScript information.

What PHP needs to do is to set the selected option without knowing about the JavaScript variable.


<?php if (($mailregion == 'Internet')) { echo "selected=\\"selected\\""; } ?>

When JavaScript runs and does its job, you can then use JavaScript to update the selected option depending on what it now knows.

Something like this should work:


var region = data[0];
$('#cities').val(
    $('#cities option:contains(' + region + ')')
);