-1

I am trying to create a weather website, where the user has 4 options from a drop down menu to select what region they want the weather for, and then from my PHP script I can use file_get_contents() to output the weather once they've selected a region and hit the submit button.

Currently, I have an error displaying: "Notice: Undefined variable: site in ... on line 12". (regarding my cases 'Horfield, 'Thornbury' etc.

I am not sure how to fix this as I have these values in my 'value' field in my tags.

here is my HTML:

<form name="Weather" method="GET" action="<? $_PHP_SELF ?>">
<select id="site" name="site">
    <option value="" disabled selected> Select a weather station...</option>
    <option value="Horfield" <php if($site == "Horfield") print('selected="selected"'); ?> Horfield (Bristol)</option>
    <option value="Thornbury" <php if($site == "Thornbury") print('selected="selected"'); ?>Thornbury (Bristol)</option>
    <option value="Glouc" <php if($site == "Gloucestershire") print('selected="selected"'); ?>Gloucestershire</option>
    <option value="Newquay" <php if($site == "Newquay") print('selected="selected"'); ?>Newquay (Cornwall)</option>
</select>
<label for="submit"></label>
<input type="submit" id="submit" name="submit">

and here is my PHP file:

<?php
$horfield = file_get_contents("http://www.martynhicks.uk/weather/clientraw.txt");
$thornbury = file_get_contents("http://www.thornburyweather.co.uk/weatherstation/clientraw.txt");
$gloucestershire = file_get_contents("https://www.glosweather.com/clientraw.txt");
$newquay = file_get_contents("http://www.newquayweather.com/clientraw.txt");
if (isset($_GET['site'])){
$site = $_GET['site'];
}

switch ($site) {
case 'Horfield':
echo $horfield;
break;

case 'Thornbury':
echo $thornbury;
break;

case 'Glouc':
echo $gloucestershire;
break;

case 'Newquay':
echo $newquay;
break;
}
?>

I'm sorry if this question seems trivial.

Also, any comments on my lines would be great:

<option value="Horfield" <php if($site == "Horfield") print('selected="selected"'); ?> Horfield (Bristol)</option>

any help is appriciated!

thanks

Luca
  • 25
  • 3
  • That form `action` attribute is entirely unnecessary, the default is to submit to self. – El_Vanja Nov 23 '20 at 14:36
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – El_Vanja Nov 23 '20 at 14:37

1 Answers1

0

I was initially perplexed how you're not getting the site value from the query string. I tried plugging your HTML code (minus PHP) via donor site (using browser's Dev Tools) and the request it made had the site value (eg, GET https://www.google.com/?site=Horfield&submit=Submit). Perhaps the issue was a failed installation, or misconfigured, PHP setup.

But then it occurred to me that both HTML and PHP code/files could "belong" to a single URL. Meaning, whether I visit the page, or submit the form, the PHP code will be executed. If that is the case, then the issue is merely due to referencing an uninitialized/undeclared variable.

This is what's causing the issue:

if (isset($_GET['site'])){
    $site = $_GET['site'];
}

If you visit the page (ie, not submit the form>, there will be no query string; thus $_GET will be empty (ie, $_GET = []) and isset() will return false. Not only will the $site variable not be set, it won't even be declared. And when it's referenced in the switch() it'll throw a NOTICE.

To fix the issue, you must always declare the $site variable:

$site = isset($_GET['site']) ? $_GET['site'] : null;  // or false or "unknown"
// Or if you're using >=PHP7, you can use the null coalescing operator ??
$site = $_GET['site'] ?? null;  // or false or "unknown"

Of course, if you need to handle this event, don't forget to set the default in your switch().


Bonus: In your HTML code, avoid unnecessary repetitions, eg:

<!--
$selected = "selected=\"selected\"";
$sites = [
    [
        "value" => "Horfield",
        "selected" => $site === "Horfield" ? $selected : "";
        "text" => "Horfield (Bristol)",
    ],
    ...
];
-->

<form name="Weather" method="GET" action="<? $_PHP_SELF ?>">
<select id="site" name="site">
    <option value="" disabled selected> Select a weather station...</option>
    <?php foreach ($sites as $site) { ?>
    <option value="<?php echo $site["value"] ?>" <?php echo "$site["selected"] ?>><?php echo $site["text"] ?></option>
    <?php } ?>
</select>
<label for="submit"></label>
<input type="submit" id="submit" name="submit">

Of course it's still better to use a templating system except for the very basic page.

loydg
  • 229
  • 2
  • 3