Show DB record without refresh [duplicate]

0
// esta é uma pagina separada, chamada adicionar_consulta.php 
<?php
$conexao = new mysqli("localhost","root","","hospital"); 

if ($conexao->connect_errno) {
echo "Failed to connect to MySQL: (" . $conexao->connect_errno . ") " . 
$conexao->connect_error;
}


$res = $conexao->query("SELECT nome,idade 
                    FROM paciente, ficha_atendimento 
                    WHERE paciente.id = ficha_atendimento.id_paciente 
                    AND ficha_atendimento.id IN (SELECT MIN(id) FROM 
ficha_atendimento)
                    AND ficha_atendimento.status IS TRUE");

while ($row = $res->fetch_assoc()) {

echo " id = " . $row['nome'] . "  " . $row['idade']. "</br>";
}

?>

I want to display the data of this query in PHP on the same page as the calling button. This code above is the query script. By pressing the button it is performed, but a redirect to another page occurs.

    
asked by anonymous 23.05.2017 / 16:53

3 answers

3

Using ajax

On your page where you have the button, enter the following code:

   $("#idDoBotao").click(function(){
   $.ajax({
       url:"adicionar_consulta.php ",
       type:"POST",
       data: ({}), //Como não está enviando nenhuma informação, pode deixar vazio
        success:function(resposta){
           $('#resultado').html(resposta);
        }

           });
            });

Considerations

I'm considering that your button has id "idDoBotao" and that you have a div > with id "resultado"

Operation

By clicking on the button with idDoBotao it will execute the function with ajax. After you run your php code (and succeed) it will write the result into your div with id result

Passing parameters in ajax

If you want to send some value per form, you can get this value! Let's suppose I have the following input: <input type="text" name="valor" value="foo" /> The procedure in the code would be as follows:

   $("#idDoBotao").click(function(){
   $.ajax({
       url:"adicionar_consulta.php ",
       type:"POST",
       data: ({valor:$("input[name='valor']").val()}), //estamos enviando o valor do input
        success:function(resposta){
           $('#resultado').html(resposta);
        }

           });
            });

And of course, we'd need to get this value in php:

$valor = $_POST['valor']; 
echo $valor; // retornará "foo"
    
23.05.2017 / 17:08
-1

$.ajax({
   type: 'POST',
   URL: 'urlDoSeuPHP',
   data: {
      // valores a ser passado
   },
   success: function(data){
      // Aqui voce trata o retorno
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
23.05.2017 / 17:07
-2

Best way to do this is with Ajax. There is enough tutorial on the internet, and you can simplify using jQuery Ajax. Take a look here: link

To make it easier, it's cool that your query page already brings the result formatted in HTML, and then you just insert it into a specific id. The site I passed is well didactic but you will find a lot of information about Ajax and jQuery on the internet. Good luck!

    
23.05.2017 / 17:04