-1

I need a piece of code that causes the user to put a name on a label and then click the search button, the user is redirected to a page of the name that the user searched for.

//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);

When I try this, it will always redirect to the last "if" (Sarzedas).

aynber
  • 22,380
  • 8
  • 50
  • 63
Darties
  • 13
  • 2

1 Answers1

0

You have issues with the = and == operators. If you want the IF() to work properly, you have to use the == operator.

Code should be like that:

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');
    }
}

For further info check this question on stackoverflow

Kaiser
  • 1,957
  • 1
  • 19
  • 28