Doubt about POST and GET in php

Asked

Viewed 328 times

4

I tried searching the internet, but I couldn’t satisfy my doubt... I started programming web recently, although I have already used post and get, I found myself in a situation where I was "stuck". I have the signup page.php , which is nothing more than a form with text fields for street, neighborhood, zip code and etc... Except that this form has 2 buttons, the Search zip, which opens search_cep.php that searches the zip that the user typed and saved in Sesssions, and returns to register.php with the other fields already filled, but also has the save button, that sends the typed values into the BD... The problem is, the zip code field is in a , POST method, for the search_cep.php, while the other fields are in the save.php Also via post, so I can’t save the zip code in the BD.... I wonder, if I could put everything in one , or send the cep via get to the search.php, since it is small information and does not need privacy...

  • 1

    Put your code or part of it to better understand.

  • It would be simpler for the search and save functionality in the same PHP. Then, depending on the pressed Ubmit button, you choose the procedure to take.

  • GET is used to send parameters through the url, and can be retrieved through $_REQUEST['_param']; or $_GET['_param']; for example: <a href="that.php? p=_param">LINK</a> can be recovered in the.php file using print_r($_GET['_param']) or $_REQUEST['_param'];

1 answer

6


A possible solution would be to use this structure:

Página PHP com o formulário 
|
+- If cliente clicou em "buscar cep"
|  |
|  +- Include "buscacep.php"
|     Que busca o endereço do cep e substitui as variáveis pegas com POST
|     Continua normalmente até mostrar o form
|
+- Else If cliente clicou em "enviar"
|  |
|  +- Include "salvanodb.php"
|     Que verifica se está tudo em ordem.
|     |
|     +- Se estiver, salva, redireciona para página de sucesso ( e die(); )
|     |
|     +- Se estiver faltando algo, guarda mensagem de erro em uma variável.
|        Nesse caso, nada de die(); que é pra continuar no form
|
+- Mostra o formulário, com os valores anteriormente digitados no value="" dos inputs
  • 1

    The "Only MVC" purists will love this answer :D

Browser other questions tagged

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