Dynamic data display with PHP

2

I'm developing a website, where I have several products, for example and a button for each of them.

When I click the button, it opens a modal (Bootstrap).

But, I want to generate the contents of this modal, or the entire modal even dynamically with a function in PHP

For example, a product with data

Nome: Carro,
Valor: 3,000.

Then I send the information to the function:

gerarmodal($nome, $valor);

And it returns me the modal, or the content of it, with the values of the variables in their proper places.

"You are buying a Car for the value of 3,000 reais."

I know you need to use javascript along with php, to request the data without refresh. I've tried but I can not come up with a logic to make it work.

How could I do this and view the modal on my page?

    
asked by anonymous 26.03.2015 / 03:52

2 answers

1

It's quite simple to actually work with the combination of html, ajax and php to make requests, see the example below:

One thing to do is to put a field inside the modal where you will receive this information, in my case I used a div called content:

 <div id="conteudo"></div>

2 you already have a button that on the click opens modal, in addition to opening the modal you should call an ajax that will send information to php, I created a function that searches for cars informing the minimum and maximum value of each:


 $("#botao").click(function(){ // no click do botao que abre a modal faça
            buscaCarro(3000,15000);
     });

function buscaCarro(min, max) {
   $.ajax({
      type: "POST",
      url: 'arquivo.php',
      dataType: 'json',
      data: 'acao=carro' + '&min=' + min + '&max=' + max,
      success: function (data) {
         console.log(data);
         // resposta do php
        $("#conteudo").append(data); // colocando o conteudo na modal
 // dependendo do conteudo pode ser html(),text()...

      },
      error: function (data) {
         alert(data.responseText);
      }
   });
}

Ajax accesses the php file and does the following action:


 if($_REQUEST['acao'] === "carro"):
      $output = buscaCarro($_REQUEST['min'],$_REQUEST['max']);
 endif;

 echo json_encode($output);

This is a basic example, I advise you to get a basic handout: Handouts - Ajax Php

    
26.03.2015 / 12:52
0

Friend, come on, I think the path is this:

A PHP file that will return the information of a product, where, such a file will receive the product ID, for example.

A little JavaScript (and jQuery) that will make the request in the PHP file and, after receiving the data, will include such values in the modal.

Come on:

query.php

$id = $_POST['id'];

// faça sua query aqui e adicione em $dadosRetornados

echo json_encode($dadosRetornados);

funcoes.js

Now you must make a function that receives the product ID and uses it in the request that will query. Something like this: (idProduct will be the value parameterized)

$.ajax({
    type: "POST",
    url: "consulta.php",
    data: { id: idProduto },
    success: function (resposta) {
        var dados = JSON.parse(resposta);
        $('#conteudoModal').html('Produto: ' + dados.nome + 'Valor: ' + dados.valor);
}

});

I believe this is the solution you are looking for, just change your HTML and add the function that will make the modal call.

    
26.03.2015 / 12:52