Add a fixed word to a friendly URL

2

Good evening,

I'm doing a search and I'm having a problem passing the value that is written in input to another page where the result will be displayed.

Url example I'm trying to pass values

http://exemplo.com/locais/pesquisa?q=bares

.htaccess file

RewriteRule ^locais/([a-zA-Z-0-9-_]+)$  index.php?controller=pesquisa?q=$1

But it always returns the search name and not the one I typed in input

Form

<form method="post" id="pesquisa_home" name="pesquisa_home" action="locais/pesquisa/<?= $_POST['valor_pesquisa'] ?>" >
          <div style="margin-left:150px;">
              <div style="float:left; width:500px;"><input id="valor_pesquisa" name="valor_pesquisa" type="text" placeholder="Restaurantes, bares, hotéis..." /></div>
              <div style="float:left; margin-left:5px;"><input type="submit" value="Pesquisar" /></div>
          </div> 
        </form>
    
asked by anonymous 18.03.2015 / 02:35

2 answers

3

Your ^locais/([a-zA-Z-0-9-_]+)$ rule says that what you have after locais/ in case pesquisa will be the value of q that receives the first value $1 .

In case your URL http://exemplo.com/locais/pesquisa?q=bares said that the variable q receives bares , but its rule is changing the value to pesquisa which is the term just after / of the rule.

You can correct by changing the rule to: ^locais/pesquisa/([a-zA-Z-0-9-_]+)$ and your URL getting http://exemplo.com/locais/pesquisa/bares

Or just taking the term pesquisa from the URL as already said by @Daniel Omine, getting: http://exemplo.com/locais/bares .

    
18.03.2015 / 02:47
0

Solution

<form method="post" id="form" action="">
<div style="margin-left:150px;">
    <div style="float:left; width:500px;"><input style="height: 38px;" id="valor_pesquisa" name="valor_pesquisa" type="text" placeholder="Restaurantes, bares, hotéis..." /></div>
    <div style="float:left; margin-left:5px;"><input type="submit" value="Pesquisar" /></div>
</div> 
</form>
<script>
$(function() {
    $("#form").submit(function(e) {
        if($('#valor_pesquisa').val() === ''){
            $("#form").attr("action", "http://sabeonde.pt/locais/pesquisa/todos");
        }else{
          var valor_pesquisa = $("#valor_pesquisa").val();
          $("#form").attr("action", "http://sabeonde.pt/locais/pesquisa/"+valor_pesquisa);
        }
    });
});

    
18.03.2015 / 05:18