为什么搜索总是重定向到最后?

时间:2019-07-09 19:34:13

标签: php html

我需要一段代码,使用户在标签上贴上一个名称,然后单击搜索按钮,将用户重定向到该用户所搜索名称的页面。

//code to redirect
if (isset($_POST["procurar"])) 
    {
        $myPelourinho = $_POST['myPelourinho'];

        if ($myPelourinho="Oleiros") {
            header('location: oleiros.php');
        }
        if ($myPelourinho="Sertã") {
            header('location: serta.php');
        }
        if ($myPelourinho="Fundão") {
            header('location: fundao.php');
        }
        if ($myPelourinho="Proença-a-Nova") {
            header('location: proenca.php');
        }
        if ($myPelourinho="Vila Velha de Ródão") {
            header('location: vilavelhaderodao.php');
        }
        if ($myPelourinho="Sarzedas") {
            header('location: sarzedas.php');
        }
    }
<!-- autocomplete and button -->
<form autocomplete="off" method="post" action="server.php" >
                <div class="autocomplete" style="width:245px;">
                    <input id="myInput" type="text" name="myPelourinho" placeholder="Localidade">
                </div>
                <button type="submit" name="procurar" class="btn btn-default" stlye="width:50px">
                    <span class="glyphicon glyphicon-search"></span>
                </button>
            </form>

<!-- autocomplete array list -->
var lista_pelourinhos = ["Fundão", "Proença-a-Nova", "Sarzedas", "Vila Velha de Ródão", "Oleiros", "Sertã"];
autocomplete(document.getElementById("myInput"), lista_pelourinhos);

当我尝试此操作时,它将始终重定向到最后的“ if”(Sarzedas)。

1 个答案:

答案 0 :(得分:0)

= == 运算符存在问题。如果希望IF()正常运行,则必须使用 == 运算符。

代码应如下所示:

if (isset($_POST["procurar"])) 
{
    $myPelourinho = $_POST['myPelourinho'];

    if ($myPelourinho=="Oleiros") {
        header('location: oleiros.php');
    }
    if ($myPelourinho=="Sertã") {
        header('location: serta.php');
    }
    if ($myPelourinho=="Fundão") {
        header('location: fundao.php');
    }
    if ($myPelourinho=="Proença-a-Nova") {
        header('location: proenca.php');
    }
    if ($myPelourinho=="Vila Velha de Ródão") {
        header('location: vilavelhaderodao.php');
    }
    if ($myPelourinho=="Sarzedas") {
        header('location: sarzedas.php');
    }
}

有关更多信息,请检查this question on stackoverflow