Error in Ajax request

Asked

Viewed 463 times

0

I need to send the value of a table that is in a modal to the server and then open another modal using the information from the first table, but the variable always returns me null, I tried to use ajax but it didn’t work, it does nothing and doesn’t give me any feedback, follows the code for better understanding of the problem:

MODAL PASSING INFORMATION:

<form id="visualizarAparelhos" name="visualizarAparelhos" method="post" enctype="multipart/form-data">
    <table class="table table">
      <thead> 
        <tr>
        <th>Número</th>
        <th>Nome</th>
        <tbody>
          <?php
            $con = mysqli_connect("localhost","roberto","","manutencao");
            $query = ("select max(id) from aparelhos");
            $max = mysqli_query($con,$query);
            $fetch = mysqli_fetch_row($max);
            $max = $fetch[0];
            $i = 0;
              while ($i != $max)
              {
                $i++;
                $con = mysqli_connect("localhost","roberto","","manutencao");
                $query = ("select id,nome from aparelhos where id = ".$i." ");
                $aparelho = mysqli_query($con,$query);
                $fetch = mysqli_fetch_row($aparelho);
                $id = $fetch[0];
                $nome = $fetch[1];
                print "<tr>";
                // PRECISO PASSAR O IdAparelho para o segundo modal
                  print "<td id='IdAparelho' value='".$id."'><button type='button' class='btn btn-primary' data-toggle='modal' data-target='#modalVisualizarComponentes'>".$id."</button></td>"; 
                  print "<td><strong>".$nome."</strong></td>";
                print "</tr>";
              }
          ?>
        </tbody>   
        </tr>
      </thead>   
    </table>
    </form> 

MODAL RECEIVING THE INFORMATION:

<div class="modal-body">
    <table class="table table-striped">
        <tr>
        <th>Sequencial</th>
        <th>Código</th>
        <th>Nome</th>
        <th>Entrada</th>
        <th>Saída</th>
        <th>Quantidade F</th>
        <th>Quantidade M</th>
        <th>Quantidade G</th>
        <th>Quantidade GG</th>
        <th>Total</th>
        <tbody>
          <?php
            $con = mysqli_connect("localhost","roberto","","manutencao");
            // VOU USAR A INFORMAÇÃO DO OUTRO MODAL AQUI
            $query = ("select id_principal from componentes where id_secundario = ".$_POST['IdAparelho']." ");
            $ids = mysqli_query($con,$query);
            while ($linha = mysqli_fetch_array($ids))
            {
                $con = mysqli_connect("localhost","roberto","","manutencao");
                $query = ("select * from componentes where id_principal = ".$linha[0]." ");
                $componentes = mysqli_query($con,$query);
                $resultado = mysqli_fetch_row($componentes);

                $id_componente = $resultado[1];
                $codigo = $resultado[2];
                $nome = $resultado[3];
                $entrada = $resultado[4];
                $saida = $resultado[5];
                $f = $resultado[6];
                $m = $resultado[7];
                $g = $resultado[8];
                $gg = $resultado[9];
                $total = ($f + $m + $g + $gg);

                print "<tr>";
                  print "<td><strong>".$id_componente."</strong></td>";
                  print "<td><strong>".$codigo."</strong></td>";
                  print "<td><strong>".$nome."</strong></td>";
                  print "<td><strong>".$entrada."</strong></td>";
                  print "<td><strong>".$saida."</strong></td>";
                  print "<td><strong>".$f."</strong></td>";
                  print "<td><strong>".$m."</strong></td>";
                  print "<td><strong>".$g."</strong></td>";
                  print "<td><strong>".$gg."</strong></td>";
                  print "<td><strong>".$total."</strong></td>";
                print "</tr>";
              }
          ?>
        </tbody>   
        </tr>
      </thead>   
    </table>    
  </div>

MY AJAX:

 $(function () {    
        $("#visualizarAparelhos").submit(function (e) {
        e.preventDefault();
        var FormData = $($this).serialize(); 
            $.ajax({
              type: "POST", 
              url: "administrador.php",
              data: FormData
            }).done(function (data) {
                alert(data);
            });
        return false;
        alert("Não funfou!!!");
        }); 
    });
  • Adminstrator.php is receiving Idaparelho?

  • Ahamm, in case the same php page you are sending is the one you will receive.

  • Always falls in the Success? Have you tried to implement the error in your Ajax code and inspect if any errors are happening?

  • I edited my ajax, and now always falls in the done, put to show the Alert of my date, but does not show the Idaparelho in Alert, I think is not sending precisely the information I need.

  • Serialize takes data from a table? As far as I know only inputs...

  • then I would use?

Show 1 more comment

1 answer

1


In this case, as it is a table, it is not possible to serialize. If you need to pass only the ID, you can do so:

        $.ajax({
            type: "POST", 
            url: "administrador.php",
            dataType: 'json',
            data: {IdAparelho: $('#IdAparelho').val()}
        }).done(function (data) {
            alert(data);
        });
  • I did so, just because it never falls in Alert, just does nothing and the log has no error.

  • I changed dataType from json to html, and it worked well

Browser other questions tagged

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