0

here is my code:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<?php
$url = "http://api.wunderground.com/api/7d5339867b063fd0/geolookup/conditions/q/UK/".$area.".json";
?>

<script>

jQuery(document).ready(function($) {
  $.ajax({
 url : ,
dataType : "jsonp",
success : function(parsed_json) {
var location = parsed_json['location']['city'];
var temp_f = parsed_json['current_observation']['temp_f'];
alert("This is an example of a web service outputting using Json. The current temperature in " + location + " is: " + temp_f);
}
});
});

</script>

How can i add the "$url" variable into the javascript code at the location " url : "here",

Thanks

SteCarter93
  • 49
  • 1
  • 8

4 Answers4

3

You just need to put it like that:

url : "<?=$url?>",

OR (best way):

url : "<?php echo $url; ?>",
Magicprog.fr
  • 4,072
  • 4
  • 26
  • 35
3
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <?php
    $url = "http://api.wunderground.com/api/7d5339867b063fd0/geolookup/conditions/q/UK/".$area.".json";
    ?>

    <script>

    jQuery(document).ready(function($) {
      $.ajax({
     url : "<?=$url?>",
    dataType : "jsonp",
    success : function(parsed_json) {
    var location = parsed_json['location']['city'];
    var temp_f = parsed_json['current_observation']['temp_f'];
    alert("This is an example of a web service outputting using Json. The current temperature in " + location + " is: " + temp_f);
    }
    });
    });

    </script>
Mahendra Jella
  • 5,450
  • 1
  • 33
  • 38
1
url : "<?php print $url ?>",
friedi
  • 4,350
  • 1
  • 13
  • 19
1

Use json_encode():

var jsVar = <?=json_encode($phpVar)?>;

or in your case:

$.ajax({
    url: <?=json_encode($url)?>,
    dataType: "jsonp"
    // ...
});

This is the safest way, and works with any data (arrays etc.).

Dávid Horváth
  • 4,050
  • 1
  • 20
  • 34