Search code examples
phpformsurlget

Why $_GET is not getting data from a different page?


I have a form in a page and I redirect to another on form submission. I need to get the data from that form in the other page, but instead, I'm only getting the new page.

<form action='/busca' method="get">
   <input class='search__input' type='text' placeholder='Buscar por notícia' name='termo'>
</form>

and then inside the 'busca' page:

    $resultados = 0;
    var_dump($_GET);
    if( isset($_GET['termo']) ) {
      $noticias = getNoticias()->rset;
      $resultados = array();
      $termo = $_GET['termo'];
      $counter = 0;
      foreach ($noticias as $id => $noticia) {
         if (strpos($noticia['titulo'], $termo) !== false) {
             $resultados[$counter]->titulo = $noticia['titulo'];
             $resultados[$counter]->id = $noticia['id'];
         }
      }
    }

When I type 'test' in the form, it redirects me to the page busca with the URL http://localhost/escolas?termo=test. But I don't get that data with my code. When I var_dump the $_GET it gives me:

array(1) { ["params"]=> string(5) "busca" }

But I need the 'termo' (in this case, 'test').

What can I do?


Solution

  • Decided to use POST instead. It works.