Add a fixed word to a friendly URL

Asked

Viewed 161 times

2

Good night,

I’m doing some research and I’m having a problem passing the value that is written on input to another page where the result will be displayed.

Example Url I’m trying to pass values

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

File . htaccess

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

But always returns me the name search 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>
  • use the url like this: http://exemplo.com/locais/bars

  • but you can’t use it that way ?

  • is possible but you need to change the rewriterule rule to ignore the word search.. I preferred not to comment on this because I suggest keeping the url as short as possible.. The word "pequisa" becomes unnecessary because it is only the name of the parameter. But if you have a specific reason, then just include the term in the rule

  • But you can’t pass the folder q as in the url it looks like http://exemplo.com/locais/search?q=bars

  • but what’s the point of sending the parameter this way ? After all you want friendly url or normal url ?

  • as I commented above, it is possible to do this... I think you want to do it is not url friendly, it is merely a "routing".

  • You’re right I’m sorry at this hour and programming day and night I’m out of my mind

  • I want and url friendly

  • If the term research is fixed, just need to put in your rule.

  • take the right search ? and leave only local/bars ?

  • 1

    would look like this: Rewriterule local/search/([a-za-Z-0-9-_]+)? index.php? controller=search? q=$1

  • then you access the link like this: address.do.site/places/search/bars

  • but now when searching it always adds the ? in the url so breaks the url

Show 8 more comments

2 answers

3


Your rule ^locais/([a-zA-Z-0-9-_]+)$ says whatever you have after locais/ in the case pesquisa will be the value of the variable 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 your rule is changing the value to pesquisa which is the term shortly after the / 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 URL as already said by @Daniel Omine, getting: http://exemplo.com/locais/bares.

  • And that’s both right I put the right one to who ? because I didn’t even realize who put it first I think it was Daniel Omine but he didn’t put it in answer

  • but now when searching it always adds the ? in the url so breaks the url

  • Although the intention of the answer is good, I think it does not solve the problem of the author, because apparently it is the GET method, which always has the '?q='. It is no use to want to use friendly URL in this case (which, in turn, is already a question problem). Probably the @Danielomine solution is the one that applies.

  • I disagree with Baccus, because it is not even clear what the author of the question wants. It is ambiguous... I believe he wants to keep the normal query usage in the URL and just add a virtual route with the word "search". But it speaks of URL friendly making the subject ambiguous... So the author is who can define it but it shows very confusing

  • @Danielomine If you disagree, show me how to use Kadu’s GET solution for me (with friendly URL), and I apologize. I like to learn new things.

  • @Danielomine Here’s what you think, maybe you can improve this idea: http://answall.com/questions/54791/problema-com-submit-de-form?noredirect=1#comment112338_54791

  • Still can’t figure out why we can’t get that ? out of Ubmit

  • @Césarsousa if Voce uses form with GET, the browser needs so in the URL, is the default method. If you do not use GET, you need to explain to us how you are doing it. I would suggest adding in the question the <form line>.

  • I already added how I am trying to do it now it doesn’t show ? but it shows the value I put in the input

  • Césarsousa has now improved to understand what you want, but is bars a subcategory, or is it what the person typed? You want a POST with subcategories. Then it got more interesting, because you don’t need ? anywhere. @Danielomine tá acompanhando? I think the problem’s gotten more interesting.

  • bars and what the person typed what could be anything else

  • Can you see now what I’m trying to say I’m not using ? i want what the person typed to be sent like this example.pt/places/search/what the person typed realized ?

  • hehe has now complicated more. Do you want to send by the POST method ? First, when sending by the POST method you do not need friendly URL. But I still think you are causing another confusion of terms referring to "data posting" using the term "POST" rather than referring to the POST method itself.

  • lol... I think I’m almost there.. you say this question mark is automatically added but you don’t want to have the question, right?

  • 1

    hence vc talks about wanting to send by the POST method (is what Pressuponho), so I think the problem is that it remains to define the sending method in the form <form method="POST" action="...">

  • I just solved it

  • @Danielomine well noted the problem of terminology. I probably should have exemplified the POST instead of just mentioning. But the good news is that he did, I think after his comment.

  • It wasn’t through the suggestion of Charlie Pope

Show 13 more comments

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

  • Just as a complement: In this case it is good to always check the value of $_POST['valor_pesquisa'] inside PHP, because even if the URL doesn’t change (in the case of JS turned off) it will work fine. (I commented this in case any reader thinks to use the solution by taking the term only from the URL)

Browser other questions tagged

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