Keep testing until a variable is created in php

Asked

Viewed 64 times

-4

I am sending an ajax to a table, but this table can only be created after receiving the value, but it is being created before in white, as I can make a check for the table to be generated only after receiving the ajax value, stay in a loop testing the variable to interrupt the php code until the variable receives its correct value.

I tried that and it wasn’t:

 if (isset($_POST['IdAparelho']))
            {
              $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);

                  $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>";
                }
            }
          ?>

AJAX CODE:

     $(function () {    
        $("#visualizarAparelhos").submit(function (e) {
        var index = $("#IdAparelho").attr('data-index');
        e.preventDefault();
        $.ajax({
          type: "POST", 
          url: "administrador.php",
          dataType: 'html',
          data: {IdAparelho: index}
    }).done(function (data) {
        console.log(data);
    }); 
    });
    });
  • Please show the code of the table creation.

  • I edited the post

  • This is being sent using Ajax: $_POST['IdAparelho']? If yes, you have already checked if there is any value within this variable?

  • I checked yes, the problem is that the variable only ceases to be null, after the table has already been created.

  • Roberto, in your javascript function put a console.log(index) to see if this variable has any value before sending it to php.

  • I did the test, it is passing the value 1.

Show 2 more comments

1 answer

0

1° - you open a connection for each query line "select id_principal from componentes where id_secundario = ".$_POST['IdAparelho']." I suggest you open 1 connection only and close it after the loop

2° - this situation could not be resolved with

if (isset($_POST['IdAparelho']))
        {
          $query = ("select * from componentes where id_secundario = ".$_POST['IdAparelho']." ");
          $ids = mysqli_query($con,$query);
          while ($linha = mysqli_fetch_array($ids))
          {

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

3º - Where you "test the variable that will interrupt the php code" ?

  • if (isset($_POST['Idaparelho'])) , <------ here, I can only create the table after $_POST['Idaparelho'] already has its value.

  • You keep ordering this page until it has some value in the "$_POST['Idaparelho']" ?

  • I’m trying to do this, I haven’t got it yet.

  • Validates in Jquery if you have something to send before sending to PHP, it will prevent you from making Ns requests...

  • The problem is that after the server passes there and finds the empty variable it no longer returns, hence the code is not executed.

  • Enter the code that makes the ajax call

  • I edited the publication.

Show 3 more comments

Browser other questions tagged

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