Hej
Piszę aplikację, która polega na wpisaniu jakiegoś miasta, a ona wypiszę pogodę.
Mam problem z PHP wypisuje mi takie dwa błędy:
Notice: Undefined index: city in C:\xampp\htdocs\myPHP\projectPHP\index.php on line 3
Notice: Undefined variable: weather in C:\xampp\htdocs\myPHP\projectPHP\index.php on line 101
Nie wiem czy to jest wina kodu czy czegoś innego. Robię ta aplikacje na lokalnym serwerze (XAMPP) może to jest przyczyną.
<?php
if($_GET['city'])
{
$forecastPage = file_get_contents("http://www.weather-forecast.com/locations/London/forecasts/latest");
$PageArray = explode ('3 Day Weather Forecast Summary:</b><span class="read-more-small"><span class="read-more-content"> <span class="phrase">', $forecastPage);
$SecondPageArray = explode ('</span></span></span>', $PageArray [1]);
$weather = $SecondPageArray[0];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Weather Scraper</title>
<style type="text/css">
html
{
background: url(background.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
body
{
background: none;
}
#container
{
text-align: center;
margin-top: 300px;
}
h1
{
font-size: 50px;
margin: 0;
}
h2
{
margin: 0;
}
#city
{
border:none;
padding: 10px;
width: 450px;
border-radius: 5px;
margin-top: 30px;
}
#button
{
border:none;
border-radius: 5px;
margin-top: 20px;
font-size: 25px;
padding: 5px 10px;
background-color:#138cc4;
}
#button:hover
{
background-color:#36a1d2;
}
#weather
{
background-color: #36d162;
border-radius: 5px;
width: 450px;
margin: 0 auto;
color: #1e7235;
}
</style>
</head>
<body>
<div id="container">
<h1>What's The Weather?</h1>
<h2>Enter the name of a city.</h2>
<form method="get">
<input type="text" id="city" name="city" placeholder="Eg. London, Tokyo">
<p><button type="button" id="button">Submit</button></p>
</form>
<div><?php
if ($weather)
{
echo '<div id="weather">'.$weather.'</div>';
}
?></div>
</div>
</body>
</html>