How to pass parameter to modal, query Mysql and return in html

Asked

Viewed 1,034 times

2

I’m trying to pass a variable to a modal, perform a query to give a feedback on html, because the data will be formatted in a table. I tried some suggestions as alternatives, but I’m having difficulty to make the thing work, I have this line that invokes the modal:

<a href="#ModalDetalhes" data-toggle="modal" id="<?php echo $Eventos->IdEvento; ?>" data-target="#edit-modal"><?php echo $Eventos->Data; ?></a>

The script that will send the variable is like this:

$('#edit-modal').on('show.bs.modal', function(e) {

  var $modal = $(this),

    IdEvento = e.relatedTarget.id;

    console.log("EVENTO: " + IdEvento);

  $.ajax({
    cache: false,
    type: 'POST',
    url: 'pDetalhesEventos.php',
    data: 'IdEvento=' + IdEvento,
    dataType: 'html',
    success: function(data) {
      $modal.find('.edit-content').html();
    }
  });
})

php is thus configured:

require_once "../_classes/conexao_pdo.class.php";
require_once "../_classes/crud.class.php";

// Atribui uma conexão PDO   
$pdo = Conexao::getInstance();
// Atribui uma instância da classe Crud, passando como parâmetro a conexão PDO e o nome da tabela  
$crud = Crud::getInstance($pdo, 'cadEventos');

// DADOS DO FORMULÁRIO
$IdEvento = (isset($_GET['IdEvento'])) ? $_GET['IdEvento'] : '';

// INICIALIZANDO O ARRAY
$arrayParam = array(); 

// BUSCA COM FILTRO 
$sql = "SELECT *, DATE_FORMAT(Data, '%d/%m/%Y') as Data FROM cadEventos WHERE IdEvento = ?";             
$arrayParam  = array($IdEvento);
$Resultado  = $crud->getSQLGeneric($sql, $arrayParam, TRUE); 
$retorno = count($Resultado);

What I tried was to do this, by passing the variable through url recovering by get and post:

url: "cliente.php?id=123",

The modal is like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<a href="#edit-modal" data-toggle="modal" id="<?php echo $Eventos->IdEvento; ?>" data-target="#edit-modal">#Modal</a>

<div id="edit-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Detalhes do Evento</h4>
      </div>
      <div class="modal-body edit-content">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
      </div>
    </div>
  </div>
</div>

The variable is correct because I can see it on the console.

Screenshots of the FF console:

Console

  • To call the modal or you use href or uses data-target. Substitute href="#ModalDetalhes" for href="#edit-modal".

1 answer

2


In this line within the Success function you are using . html() without passing any arguments to the . html function().

$modal.find('.edit-content').html();
  • If you really want to receive the GET variable in PHP you can change the URL snippet. url: 'pDetalhesEventos.php', for url: 'pDetalhesEventos.php?IdEvento=' + IdEvento',

  • Hello @Matheus Prado, in the Firefox console I can see the content in "Replay" but not in modal.

  • Do you say that PHP is returning in the correct way to the 'date' variable within the ajax Success function? And yet your modal is not filled?

  • Main answer has been edited, I hope that’s the problem :D

  • Thanks @Matheus Prado, a shameful mark, but your help minimized that, even value.

  • Arrange ... : D

Show 1 more comment

Browser other questions tagged

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