Create a select of cities, when choosing the city goes to the site specific to the chosen city

Asked

Viewed 372 times

0

This is my select, for a long time I try to use more do not know how it works, I see that many large sites use and the user select the city it shows the respective content, but the URL does not change.

<form action="cid.php" method="get">
    <select name="cidade" id="cidade">
        <option>Selecione</option>
        <option value="Cidade 1">Cidade 1</option>
        <option value="Cidade 2">Cidade 2</option>
        <option value="Cidade 3">Cidade 3</option>
    </select>
  <input type="submit" name="submit" id="submit" class="botao" value="Ok">
</form>

Anyone who can help or give me direction I thank you very much.

  • Are you sure the URL does not change? By pressing the button OK the URL should be something like cid.php?cidade=Cidade%201. Some current browsers omit to query string by default URL, so you already tried to recover the value in PHP with $_GET?

  • It shows "City=City+1&Ubmit=Ok", I wanted to know how to get him to the page of the given city.

  • And what page would that be?

  • The site is telecom, each city has a value, ai would be type: city 1 = page 1. As it is done on this site and many others: http://www.evonetworks.com.br/cid.php | https://www.brisanet.com.br

  • How would I make the user’s choice stick even if he changes pages? Let’s say the user accessed the site and chose your city, but after switching pages on the same site, the choice remains. I did as mentioned above and it worked, however, when the user navigates to other pages, it gets lost.

1 answer

1


You can put the HTML like this:

<form action="cid.php" method="get">
    <select name="cidade" id="cidade">
        <option>Selecione</option>
        <option value="city1">Cidade 1</option>
        <option value="city2">Cidade 2</option>
        <option value="city3">Cidade 3</option>
    </select>
  <input type="submit" name="submit" id="submit" class="botao" value="Ok">
</form>

And in the file code cid.php, you can make a logic more or less like this:

<?php

    switch ($_GET['citade']) {
        case 'city1':
            header('Location: /cidade-1.php');
            exit;

        case 'city3':
            header('Location: /cidade-1.php');
            exit;

        case 'city3':
            header('Location: /cidade-1.php');
            exit;

        default:
            header('Location: /default.php');
            exit;
    }
  • It worked perfectly, now I think it’s just use ajax for the URL to keep the same to not stay: www.meusite.com.br/city-1

  • Is there any way to do GET along with my refresh ajax?

  • If you want to leave the URL fixed without using ajax, you can use <?php include('city-1.php');?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.