PHP切换案例-基于下拉菜单中的选定选项使用file_get_contents()



我正在尝试创建一个天气网站,用户可以从下拉菜单中选择4个选项来选择他们想要的天气区域,然后在我的PHP脚本中,一旦他们选择了一个区域并点击提交按钮,我就可以使用file_get_contents((来输出天气。

目前,我有一个错误显示:";注意:未定义的变量:site in。。。在线12"上;。(关于我的案例"Horfield"、"Thornbury"等

我不知道如何解决这个问题,因为我在标签的"值"字段中有这些值。

这是我的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">

这是我的PHP文件:

<?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;
}
?>

如果这个问题看起来微不足道,我很抱歉。

此外,任何关于我的评论都会很棒:

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

任何帮助都会得到通知!

感谢

一开始我很困惑为什么没有从查询字符串中获得site值。我尝试通过捐赠网站(使用浏览器的开发工具(插入您的HTML代码(不包括PHP(,它发出的请求具有site值(例如GET https://www.google.com/?site=Horfield&submit=Submit(。问题可能是安装失败,或者PHP设置错误。

但后来我想到HTML和PHP代码/文件都可以";归属;到单个URL。这意味着,无论我是访问页面,还是提交表单,PHP代码都将被执行。如果是这样的话,那么问题仅仅是由于引用了一个未初始化/未声明的变量。

这就是问题的原因:

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

如果您访问该页面(即不提交表单>,则不会有查询字符串;因此$_GET将为空(即$_GET = [](,isset()将返回false$site变量不仅不会被设置,甚至不会被声明。当它在switch()中被引用时,它将抛出一个NOTICE

要解决此问题,必须始终声明$site变量:

$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"

当然,如果您需要处理此事件,请不要忘记在switch()中设置default


好处:在HTML代码中,避免不必要的重复,例如:

<!--
$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">

当然,除了非常基本的页面之外,最好使用模板系统。

最新更新