【发布时间】:2020-11-23 14:01:51
【问题描述】:
我正在尝试创建一个天气网站,用户可以从下拉菜单中选择 4 个选项来选择他们想要天气的区域,然后从我的 PHP 脚本中我可以使用 file_get_contents() 输出一次天气他们已经选择了一个地区并点击了提交按钮。
目前,我有一个错误显示:“注意:未定义的变量:第 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;
}
?>
如果这个问题看起来微不足道,我很抱歉。
另外,我的线路上的任何 cmet 都会很棒:
<option value="Horfield" <php if($site == "Horfield") print('selected="selected"'); ?> Horfield (Bristol)</option>
任何帮助都会得到帮助!
谢谢
【问题讨论】:
-
<php if($site- 标签中缺少问号 -
那个表单
action属性完全没必要,默认是提交给self。
标签: php switch-statement