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

4

4 Answers 4

3

You just need to put it like that:

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

OR (best way):

url : "<?php echo $url; ?>",
Sign up to request clarification or add additional context in comments.

Comments

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>

2 Comments

Data from $url need to be between double quotes to be valid: "<?=$url?>"
I have put in double quotes just see pls @Magicprog.fr
1
url : "<?php print $url ?>",

Comments

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.).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.