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 the values

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

.htaccess file

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

But the search name always returns me and not what 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 the case of your%% URL, the variable http://exemplo.com/locais/pesquisa?q=bares receives q , but its rule is changing the value to bares which is the term just after pesquisa of the rule.

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

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

    
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