How to capture form field with . htaccess?

Asked

Viewed 78 times

0

I’m making my website url friendly, however, I’m having trouble capturing the form search field with .htaccess. All my attempts give 404 error.

Form:

<form action="busca" method="post">
<input type="text" name="busca" required="required" class="buscar-frm">
</form>

htaccess

RewriteRule ^busca/([0-9]+)/?$ /imv.php?busca=$1 [NC]

It was tried to use both the method post as the get. In the search field, it is only to search by property code.

When I enter the search field, it goes to page with error 404, and not to imv.php

  • Check the Apache logs for what you’re getting. When you use the GET method to submit the form, the value will appear in the URL being something like "search? search={value}" (the field will be listed, hence you should consider it when searching).

  • @Giovanninunes Still giving error 404, does not go to the page imv.php either using post or get

  • Please if I helped you, mark it as the best answer.

1 answer

2


The confusion ta no . htaccess, vc ta making a POST request and waiting for a GET. I made this code and here ta working, test ai.

.htaccess

<IfModule mod_rewrite.c>

    RewriteEngine On 

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^busca/([0-9]+)/?$ /teste/imv.php [NC,L]
</IfModule>

imv.php

<h1>Encontrado:</h1> <?php echo $_POST['busca'] ?>

index php.:

<form action="busca/1" method="post">
    <input type="text" name="busca" required="required" class="buscar-frm">
    <button type="submit"> Enviar </button>
</form>
  • Thank you so much for the solution. Perfect!

Browser other questions tagged

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