How to register, query and display data in the same request?

Asked

Viewed 58 times

0

I have a form where the user enters his data (name, experience, height and weight), the information is registered in the database, compared and is returned to the user the ideal board model according to his information, within a modal. All this is already done and working.

The problem is that, obviously, as the SELECT is already inside the code, as soon as the page is loaded the REQUEST in the bank is already done! And I have as return the last user who registered his information in the bank.

The user has to enter his data in the form, its data is registered in the database, the comparison is made and the return with the result is displayed in the SAME request.

Desta forma

So far, what have I done:

Here is done the SELECT. Comparison of data in the BD:

function BuscaAlgo($conexao){
$query = "SELECT  USU.usuario,
                 USU.nome,
                 USU.exp,
                 USU.altura,
                 USU.peso,
                 PRAN.exp_ref,
                 PRAN.altura_ref,
                 PRAN.peso_ref,
                 PRAN.tipo_prancha,
                 PRAN.tamanho_prancha, 
                 PRAN.meio_prancha, 
                 PRAN.litragem_prancha       
                  FROM DADOS_USUARIO AS USU 
                       INNER JOIN PRANCHA AS PRAN
                           on USU.exp = PRAN.exp_ref
                            WHERE USU.altura = PRAN.altura_ref
                              AND USU.peso = PRAN.peso_ref
                                ORDER BY USU.usuario DESC LIMIT 1"; 

  $resultado = mysqli_query($conexao,$query);

  $retorno = array();

  while($experiencia = mysqli_fetch_assoc($resultado)){
    $retorno[] = $experiencia;
  }
 return $resultado;
} 

Here displays the result of SELECT bank-made:

include "banco.php";    

$resultado = array();
$resultado = BuscaAlgo($conexao);

foreach($resultado as $valor){
  echo $valor['usuario']; print('.  .'); 
  echo $valor['nome']; print('.  .'); 
  echo $valor['exp']; print('.  .'); 
  echo $valor['altura']; print('.  .'); 
  echo $valor['peso']; print('.  .'); 
  print('///////');
  echo $valor['tipo_prancha']; print('.  .'); 
  echo $valor['tamanho_prancha']; print('.  .'); 
  echo $valor['meio_prancha']; print('.  .'); 
  echo $valor['litragem_prancha'];  
}

(Ignore the visual part. At this point, the goal is only to bring the same result)

How can I do that within the same request? What is the best way to do and how?

  • When do these echos Aren’t endings returned to the customer? You see something printed?

  • Yes! Those echos return me exactly what I want! It displays the SELECT that I made.

  • Why does the request to the bank soon in the page loading and not only in the submission of customer data?

  • I believe this is happening because the registration and consultation code is after the closure of </html>. You need to create a file for each action?

  • 1

    It is not necessary, just put a condition, which says to only execute this if the form is submitted: if($_SERVER['REQUEST_METHOD'] == 'POST') { Cadastrar, consultar, exibir... . I assume you’re using the method post in the right form?

  • have any js ?? For requests without changing pages I suggest you use AJAX . And if you have using Jquery use $.ajax({})

  • Yes, method POSTS. I am using AJAX to block the refresh of the page, because when the form is submitted, the data is sent, the page is updated and the modal is displayed. The modal has to be displayed without the refresh page, so AJAX.

  • I’ll update my code and bring you the feedback.

  • @Miguel Pus a if($_SERVER['REQUEST_METHOD']{, in the cadastra and consulta it works, but we echos, code that prints the result on the screen it does not work. I tried it in different ways, in different positions and nothing :/

  • Ha ok, with ajax is different. But the request only sent when you do the right form Ubmit? Then how to load other data

  • How would it be with AJAX?

  • Maybe it will help http://www.w3schools.com/php/php_ajax_database.asp

Show 7 more comments
No answers

Browser other questions tagged

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