Select without refresh page

Asked

Viewed 691 times

1

I use a select to access multiple sites example: city-1, city-2, city-3, when I select the city the url is: www.meusite.com.br/city-1. There I tried to use ajax to get the URL fixed on: www.meusite.com.br, but I was unsuccessful.

<?php 
switch ($_GET['cidade']) {
    case 'cidade-a':
        header('Location: /seletor/cidade-1.php');
        exit;

    case 'cidade-b':
        header('Location: /seletor/cidade-2.php');
        exit;

    case 'cidade-c':
        header('Location: /seletor/cidade-3.php');
        exit;
    // default:
    //     header('Location: /seletor/index.php');
    //     exit;

} ?>

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

I really appreciate anyone who can help me.

  • I couldn’t update the content without modifying the URL. Example: when selecting the city-1 continue with the URL: www.meusite.com.br instead of www.meusite.com.br/city-1.

  • I want when selecting the city it takes the file of each city without changing the URL, because when it selects it is: www.meusite.com.br/city-1.php, city-2.php, city-3.php. I want him to take the respective content without having to show the URL of which city it is, because the name of the city will be in the HEADER of the site, similar system to this site: https://www.brisanet.com.br/home/

  • I wanted to do the same as the site I sent over there on the breeze, the user selects the city and it shows the index content of the given city, not to have to show: city.php understands?

  • Usually each city is a different value, are different plans and conditions, I will upgrade my intro for you to see.

  • For you to better understand how it works I surpassed my code: http://connect.vaptsites.com.br

  • I want to show the content of the city, without showing her name in the URL.

  • I tested some cities of Brisa Net and all that tested is the same site, same content, same prices. But in your case you can add the value of the city in a session or in cookies and then you can capture that value and use a class of view to display the contents of that city to the user. The advantage of using session or cookies, is that you can browse multiple pages and not necessarily a single page. Oh, and here’s a tip: Don’t use an 11MB image as a background.

  • The breeze it changes the plans and values if you choose another state it will change. In my case is the same site also will only change some values

  • The background image I put up just to test I will optimize it is reduce, there is somewhere where I can learn to use Ssion or cookies ?

  • This... Then the user will already know the city because it will be on the menu, I made a map also that I will call with select, I’m only picking up on this redirect part that I never needed to use.

  • That... 1 PHP file for each city, there is a better solution?

  • Of course, only 1 file and you do GET to know what the name of the city was chosen

  • If you have 100 cities you will make 100 php files?

  • So that my GET is wrong, isn’t it? Because I put there to pull the PHP file of each city, however... It won’t be more than 10 cities I believe

  • Could you help me solve this problem ? Because I’m totally lost. But you already gave me a north, very obg.

Show 10 more comments

2 answers

1


You can load the selected page on select via POST after Submit. Instead of doing switch to compare the value sent by select, put us value of select the name of the pages and change the method for post:

<form action="cid.php" method="post">
    <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>

Then you will get the value of select in the $_POST and pull require:

<?php
$cidade = $_POST['cidade'];

if(!empty($cidade)){
   require('/seletor/'.$cidade.'.php');
}
?>

The whole code would look like this:

<?php
$cidade = $_POST['cidade'];

if(!empty($cidade)){
   require('/seletor/'.$cidade.'.php');
}
?>
<form action="cid.php" method="post">
    <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>
  • I will test now, I really appreciate the help, I’m already studying the Sessions that will be very useful to me.

  • did not work with the post :( http://connect.vaptsites.com.br

  • He can’t pull the . php file

  • on the server I will use without being in the selector folder, how would that look? like this? require($city. '. php');

  • @Joséfirmino You have to put the path of the folder concatenating with the $city

  • It returned content from within the city-1.php, but the content of Cid.php remained.

  • Thus getting a white line above all

  • @Joséfirmino If you don’t want to upload the old content, just make an if if the POST is full. Then you have to learn the basics of PHP and see what you want to appear on the page.

  • OK vlw I will do tests and put here.

Show 4 more comments

-1

You can not change the link, to use ajax, Voce have to change the information of the static page!

 if(cidade1){
  $('informacao1').val('suainformacao');
}else if(cidade2){
  {...}
}

I hope you understand!

Browser other questions tagged

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