【问题标题】:Allow redirect to URL not within htdocs (XAMPP)允许重定向到不在 htdocs 中的 URL (XAMPP)
【发布时间】:2023-04-08 14:28:01
【问题描述】:

真的很难让我的表单允许用户根据他们选择的单选按钮访问 URL。当我在我的服务器上完成表单时,它会按预期重定向到专用 URL,但是它说找不到对象并且还说“在此服务器上找不到请求的 URL”。我真的不明白如何将文件保存到网站 URL 的 htdocs 文件夹中。用户可能被重定向到的网站选项如下:

http://www.nisra.gov.uk/demography/default.asp28.htm

http://www.nrscotland.gov.uk/statistics-and-data/statistics/statistics-by-theme/vital-events/names/babies-first-names

http://www.ons.gov.uk/ons/rel/vsob1/baby-names--england-and-wales/index.html

以下是我的代码:

PHP:

<?php

if (isset($_GET['location']))
  {

$location = $_GET["location"];

if ($location === "S"){

header("Location:  http://www.nrscotland.gov.uk/statistics-and-data/statistics/statistics-by-theme/vital-events/names/babies-first-names");

}   else if ($location === "EW")

{
header("Location: http://www.ons.gov.uk/ons/rel/vsob1/baby-names--england-and-wales/index.html");

}   else if ($location === "NI")

{
header("Location: http://www.nisra.gov.uk/demography/default.asp28.htm");

}

}

?> 

HTML:

<!DOCTYPE html>
<head>
  <title>Baby Name Popularity Tables</title>
</head>
<body>
<h1>Baby Name Popularity Tables</h1>
Please select either England and Wales, Scotland or Northern Ireland.
<form action="" method="get">
<table>
<tr> 
<td><input type="radio" name="location" value="EW"/></td>
<td>England &amp; Wales</td>
</tr>
<tr>
<td><input type="radio" name="location" value="S"/></td>
<td>Scotland</td>
</tr>
<tr>
<td><input type="radio" name="location" value="NI"/></td>
<td>Northern Ireland</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Get Baby Names"/></td>
</tr>
</table>
</form>     
</body>
</html>

任何帮助将不胜感激。

【问题讨论】:

    标签: php html forms redirect xampp


    【解决方案1】:

    好的,首先,你应该重构你的代码,使其更加干净和精确,为此,我为你做了,但下次尝试改进:)

    redirectForm.php

    <?php
    
    $websites = [
        "S" => "http://www.nrscotland.gov.uk/statistics-and-data/statistics/statistics-by-theme/vital-events/names/babies-first-names",
        "EW" => "http://www.ons.gov.uk/ons/rel/vsob1/baby-names--england-and-wales/index.html",
        "NI" => "http://www.nisra.gov.uk/demography/default.asp28.htm",
        // "ANOTHER_PLACE" => "WEBSITE",
        // "ANOTHER_PLACE" => "WEBSITE",
    ];
    
    $location = isset($_GET['location']) ? $_GET['location'] : false;
    
    foreach ($websites as $key => $value) {
        if ($key === $location) {
            header("Location: ".$value);
        }
    }
    

    HTML(注意我在表单操作中所做的,第 8 行):

    <!DOCTYPE html>
    <head>
      <title>Baby Name Popularity Tables</title>
    </head>
    <body>
    <h1>Baby Name Popularity Tables</h1>
    Please select either England and Wales, Scotland or Northern Ireland.
    <form action="redirectForm.php" method="get">
    <table>
    <tr> 
    <td><input type="radio" name="location" value="EW"/></td>
    <td>England &amp; Wales</td>
    </tr>
    <tr>
    <td><input type="radio" name="location" value="S"/></td>
    <td>Scotland</td>
    </tr>
    <tr>
    <td><input type="radio" name="location" value="NI"/></td>
    <td>Northern Ireland</td>
    </tr>
    <tr>
    <td colspan="2"><input type="submit" value="Get Baby Names"/></td>
    </tr>
    </table>
    </form>     
    </body>
    </html>
    
    
    
    

    【讨论】:

    • 您好,感谢您的回复,但这也不起作用。在成功将用户重定向到专用 URL 但找不到对象的情况下,它的作用完全相同。不确定是什么导致了问题 tbh
    猜你喜欢
    • 2016-07-14
    • 2014-05-28
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 2010-10-27
    • 2010-10-18
    • 2018-08-17
    相关资源
    最近更新 更多