title: Checking if user inputs a valid city or country?
body: <p>I can't find a way for the if statement in the checkText function, to check if the city is invalid, Example:</p>
  
  <blockquote>
    <p>If you typed "jfeaio" or any other text that doesn't match a city name
    then it would say "Enter a valid city".</p>
  </blockquote>
  
  <p>I made a if statement in the function which checks if the input text is "notkoko", if it is then it is going to echo "Enter a valid city" just to test, and it seems to work, but i want it to say that whenever you type something that isn't a city name or a country name.</p>
  
  <p>I've tried a 2 things like:</p>
  
  <p>Using get_headers,
  Checking if the file() is false.</p>
  
  <p>The 2 things did work but, then the error would pop up no matter what, like if i typed a valid city name or not.</p>
  
  <p>This is the website</p>
  
  <pre><code>http://79.170.40.39/senixmenix.com/PHP/WeatherScraper/index.php
  </code></pre>
  
  <p>Here's the code for both files if you don't want to visit the link</p>
  
  <p>scraper.php(This one is what contains all the PHP)</p>
  
  <pre><code>&lt;?php
  
      $cityTextt = $_GET['city'];
      $result = "";
  
  
      $cityTextt = str_replace(' ', '-', $cityTextt);
      $weather = file('http://www.weather-forecast.com/locations/'.$cityTextt.'/forecasts/latest');
  
      $result = $weather[353];
  
      $headers = "";
  
  
      /*if($headers[0] == "HTTP/1.1 404 Not Found")
      {
  
          echo '&lt;div class="alert alert-danger" id="fail"&gt;You must enter a valid city.&lt;/div&gt;';
  
      }
      else
      {
          echo $result;
      }*/
  
      function checkText($cityText)
      {
          $headers = @get_headers('http://www.weather-forecast.com/locations/'.$cityTextt.'/forecasts/latest');
  
          if($headers == false)
          {
              throw new Exception('&lt;div class="alert alert-danger" id="fail"&gt;You must enter a valid city.&lt;/div&gt;');
          }
          else
          {
              return true;
          }
      }
  
      try
      {
          checkText($cityTextt);
  
          echo $result;
      }
      catch(Exception $e)
      {
          echo $e-&gt;getMessage();
      }
      //print_r($headers);
  
  ?&gt;
  </code></pre>
  
  <p>and here's the index.php(This one doesn't contain any PHP)</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
    &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
      &lt;title&gt;Jesper's Website&lt;/title&gt;
  
      &lt;!-- Bootstrap --&gt;
      &lt;link href="css/bootstrap.min.css" rel="stylesheet"&gt;
  
      &lt;!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --&gt;
      &lt;!-- WARNING: Respond.js doesn't work if you view the page via file:// --&gt;
      &lt;!--[if lt IE 9]&gt;
        &lt;script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"&gt;&lt;/script&gt;
        &lt;script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"&gt;&lt;/script&gt;
      &lt;![endif]--&gt;
  
      &lt;style type="text/css"&gt;
  
          body{
              font-family: Copperplate Gothic Light, sans-serif;
          }
  
          .large{
              font-size: 1000%;
          }
  
          .center{
              text-align: center;
          }
  
          .bold{
              font-weight: bold;
          }
  
          .marginTop{
              margin-top: 30px;
          }
  
          .marginRight{
              margin-right: 25px;
          }
  
          .title{
              margin-top: 60px;
              font-size: 350%;
          }
  
          .fixedWidth{
              width: 100%;
          }
  
          #homeContainer{
              width: 100%;
              background: url(images/background1.jpg) no-repeat center center fixed;
              background-size: cover;
          }
  
          #topRow{
              margin-top: 60px;
          }
  
          #success{
              display: none;
          }
  
          #faill{
              display: none;
          }
  
      &lt;/style&gt;
  
    &lt;/head&gt;
  
    &lt;body&gt;
  
      &lt;div class="container contentContainer" id="homeContainer"&gt;
  
          &lt;div class="row center" id="topRow"&gt;
  
              &lt;div class="col-md-6 col-md-offset-3"&gt;
  
                  &lt;h1&gt;Weather Scraper&lt;/h1&gt;
                  &lt;p class="lead"&gt;Enter your city below to get a forecast of the Weather&lt;/p&gt;
  
                  &lt;form method="post"&gt;
  
                      &lt;div class="form-group"&gt;
  
                          &lt;input type="text" placeholder="Eg. Horsens, New York, Las Vegas" name="city" id="cityText" class="form-control" /&gt;
  
                      &lt;/div&gt;
  
                      &lt;div class="alert alert-success" id="success"&gt;&lt;/div&gt;
                      &lt;div class="alert alert-danger" id="faill"&gt;Enter a city!&lt;/div&gt;
  
                      &lt;input type="submit" name="submit" value="Submit" class="btn btn-success btn-lg" id="submitBtn" /&gt;
  
                  &lt;/form&gt;
  
              &lt;/div&gt;
  
          &lt;/div&gt;
  
      &lt;/div&gt;
  
  
      &lt;!-- jQuery (necessary for Bootstrap's JavaScript plugins) --&gt;
      &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;
      &lt;!-- Include all compiled plugins (below), or include individual files as needed --&gt;
      &lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt;
  
      &lt;script type="text/javascript"&gt;
  
  
          var cityInput = "";
          var wHeight = $(window).height();
  
          $(".contentContainer").css("height", wHeight + "px");
  
          $("#submitBtn").click(function(event){
  
              $(".alert").hide();
  
              cityInput = $("#cityText").val();
  
              event.preventDefault();
  
              if(cityInput != "")
              {
                  $.get("scraper.php?city=" + cityInput, function(data){
  
                      if(data == "")
                      {
                          $("#faill").html("Enter a valid city!").slideDown(500);
                      }
                      else
                      {
                          $("#success").html(data).slideDown(500);
                      }
  
  
                  });
  
  
  
              }
              else
              {
                  $("#faill").slideDown(500);
              }
  
          });
  
  
      &lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Thanks :-)</p>