0

I am trying to create a one page app that searches weather by a zip code and displays the results. I am accessing a weather database, but I am stuck on how to append a specific json object to my dom

Incomplete javascript

 $(function() {
$("#getzip").submit(function() {
var zip_data = $(this).serialize();
    $.getJSON("get_weather.php",zip_data, function(data); {
    ("#output").append(current_obervation.temperature_string); // this is what I want it to do tho obviously incorrect


  });
 });
})

HTML

<h1>Weather</h1>
<hr />
<form method="get" action="get_weather.php" id="getzip">
<p>
  <label for="zip">ZIP:</label>
  <input type="text" name="zip" id="zip">
  <input type="submit" name="button" id="button" value="Submit" >
</p>

<pre id="output">
</pre>

PHP

<?php
 $zip = isset($_GET['zip']) ? $_GET['zip'] : $_POST['zip'];
 $weather_data = file_get_contents("http://api.wunderground.com/api/6d125bc5977276ee/conditions/q/{$zip}.json");
echo $weather_data 
?>

And a small sample of what the database returns. How would I pinpoint temperature and append it to the dom?

current_observation": {
"image": {
  "url": "http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png",
  "title": "Weather Underground",
  "link": "http://www.wunderground.com"
   "temperature_string": "53.5 F (11.9 C)",
  },
}
0

5 Answers 5

1

You need to add "output" to your html.

<div id="output"></div>

$(function() {
    $("#getzip").submit(function() {
        var zip_data = $(this).serialize();
        $.getJSON("get_weather.php",zip_data, function(data); {
             $('#output').html(data.current_observation.temperature_string);
        });
    });  
})
Sign up to request clarification or add additional context in comments.

4 Comments

it was there, it just was not formatted to correctly in my post
Change from 'pre' to 'div'.
Also, use console.log (data) and see in the console what does your get_weather.php returns to your script.
And, finally, you have ';' after 'function (data)'. Since I copied it from your script - I also have it in my answer. Remove it.
0

Try this way

$('#output').append(data.current_observation.temperature_string)

DEMO

Comments

0

Here is what you actually need.Demo

$('#output').append(data.current_observation.image.temperature_string)

Comments

0
 $.get( "get_weather.php", function( data ) {       
 $( "#output" ).append( "Temperature: " + data.current_observation.temperature_string    )},"json" );

// mentioned "json" as 3rd parameter as it emits json response

Reference from last example here .

Comments

0

Thanks for all the help but I discovered this way works best

$(function() {
$("#getzip").submit(function() {
  var zip_data = $(this).serialize();
  $.getJSON("get_weather.php", zip_data, function(data) {
    $("#output").empty();
    $('#output').append(JSON.stringify( data.current_observation.temperature_string, " "));                   
  });
  return false;
 });
});

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.