【问题标题】:PHP switch case - using file_get_contents() based on selected option from a drop down menuPHP switch case - 基于从下拉菜单中选择的选项使用 file_get_contents()
【发布时间】: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 switch-statement


【解决方案1】:

我最初很困惑您如何没有从查询字符串中获取site 值。我尝试通过捐赠站点(使用浏览器的开发工具)插入您的 HTML 代码(减去 PHP),并且它发出的请求具有 site 值(例如,GET https://www.google.com/?site=Horfield&amp;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">

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

【讨论】:

    猜你喜欢
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 2023-03-29
    • 2016-01-10
    • 2020-09-15
    相关资源
    最近更新 更多