Is there a way to display city weather information that I have selected from a drop-down menu using JSON? I am able to retrieve the city using jQuery, but I am not sure how to load the weather data for that city. Any guidance on this would be appreciated.
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="weather-container" style="background:#ccc;text-align:center">
<img src="" alt="" class="icon">
<p class="city"></p>
<p class="weather" style="font-size: 22px;margin:0"></p>
<p class="temp" style="font-size: 60px;margin:0;font-weight:bold"></p>
<select name="" class="select_city" id="">
<option value="Lahore">Lahore</option>
<option value="Karachi">Karachi</option>
<option value="Islamabad">Islamabad</option>
<option value="Perth">Perth</option>
<option value="Berlin">Berlin</option>
</select>
</div>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="script.js"></script>
script.js
$(document).ready(function(){
$("select.select_city").change(function(){
var selectedCountry = $(this).children("option:selected").val();
alert("You have selected the country - " + selectedCountry);
});
});
var cityy = "Lahore";
$.getJSON("http://api.openweathermap.org/data/2.5/weather?q=" + selectedCountry + "&units=metric&APPID=d89208ad904d31a4402384ff1d4d1a2f",
function(data){
console.log(data);
var icon = "https://openweathermap.org/img/w/" + data.weather[0].icon + ".png";
$('.icon').attr("src", icon);
var temp = Math.round(data.main.temp);
$('.temp').append(temp);
var weather = data.weather[0].main;
$('.weather').append(weather);
var city = data.name;
$('.city').append(city);
});