Request problem $.ajax - Receive HTML table

Asked

Viewed 141 times

0

I’m not getting to mount the modal getting a table using the $.ajax. Does anyone have any tips?

I’m sending him this way: JS file:

$("html").on('click', "#modalHorariosQuadra", function(){
    var idQuadra = parseInt( $(this).val() );
    var modalTela = $('#modal');    

    $.ajax({
        type: 'POST',
        dataType:'json',
        url: '../Ajax/quadraHorariosParaAlugar.php',
        data: { idQuadra : idQuadra },
        complete: function(retorno){
            modalTela.modal('show');
            modalTela.find('.modal-title').text('Escolha um horário para fazer sua reserva.');
            $('.modal-body').html(retorno);             
        }
    });     
});

The modal even appears, however, without the table: inserir a descrição da imagem aqui

The PHP file is mounting the table, the file returns me like this:

inserir a descrição da imagem aqui

In the PHP file I just assemble the table, without much mystery.

PHP file:

<?php
// INICIA UMA NOVA SESSÃO DE USUÁRIO
session_start();

// VERIFICA SE ESTÁ LOGADO
if (!isset($_SESSION["id"])){
    // SE O LOGIN NÃO AUTENTICAR                
    $_SESSION['loginError'] = "<div class='alert alert-danger'>Efetue o login!</div>";
    header("Location: ../Index.php");
} else {
    // INCLUI A CAMADA CONTROLLER
    include_once("C:/xamppNew/htdocs/futamador/Model/Model.php");
    $model = new Model();

    $idQuadra = $_POST['idQuadra'] ? $_POST['idQuadra'] : null;

    $arrayHorarios = $model->conexaoProprietario->buscaHorariosQuadra($idQuadra);

    if($arrayHorarios){ ?>

        <table class="table table-hover">
            <thead>
                <tr>
                    <th scope="col">Hora Inicio</th>
                    <th scope="col">Hora Fim</th>
                    <th scope="col">Data</th>
                    <th scope="col">Ações</th>
                </tr>
            </thead>
            <tbody>

                <?php
                    foreach ($arrayHorarios as $res){
                        $idHorario =  $res['idHorarios'];
                        $idQuadra =  $res['Quadras_idQuadras'];
                        $horaInicio =  $res['horarioInicial'];
                        $horaFim =  $res['horarioFinal'];
                        $dataDisponivel =  $res['dataHorario']; ?>

                        <tr>
                            <td><?php echo $horaInicio; ?></td>
                            <td><?php echo $horaFim; ?></td>
                            <td><?php echo $dataDisponivel; ?></td>
                            <td>
                                <button type="button" id="excluirHorarioProp" class="btn btn-danger btn-sm" value="<?php echo $idHorario; ?>">Excluir</button>
                            </td>
                        </tr>
                    <?php }
                ?>

            <tbody>
        </table>            
    <?php  } else {
        echo "<div class='alert alert-danger'>Não existe horários cadastrados para esta quadra.";
    }
}
  • Gives a console.log on return

  • Thanks for the tip, @adventistaam. He’s returning me the HTML in responseText, some idea how I display?

  • But he’s returning from the <table>... </table> normal?

  • Yes! I put return.responseText and printed in modal

  • I’m glad it worked

  • 2

    I believe the situation is due to dataType was json. As I checked, it should be text or even omitted.

  • @Gabrielheming you’re absolutely right! I took the dataType and changed the complete: for success: and it worked perfectly. Thank you very much!

Show 2 more comments
No answers

Browser other questions tagged

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