Sync php and ajax

Asked

Viewed 147 times

0

I am doing a select in php using a variable that comes from ajax, the problem is that php is trying to do the query before receiving the ajax value, this generates an error in the page and the routine is broken in the middle,

FOLLOW THE CONSOLE ERROR LOG:

<div class='modal fade' id='modalVisualizarComponentes' tabindex='-1'     role='dialog' aria-labelledby='myModal'>
<div class='modal-dialog modal-lg' role='document' style='height:80% !important; width:100% !important;'>
<div class='modal-content'><div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button><h4 class='modal-title' id='myModalLabel'>Componentes</h4></div>
<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><br />

// THIS IS WHERE I NEED THE CONSULTATION TO PUT THE FIELD THAT I NEED DOWN BELOW.

Warning: mysqli_fetch_array() expects Parameter 1 to be mysqli_result, Boolean Given in C: xampp htdocs Manutencao admin.php online 265
Come out

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js">                 </script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

<script type="text/javascript">
function validaCampoAparelho()
{
  if (document.aparelho.labelAparelho.value=="")
  {
    alert("O campo aparelho está em branco!!");
    return;
  }
}
$("#visualizarAparelhos").submit(function (e) {
    //e.preventDefault();
    var dados = $("#visualizarAparelhos").serialize();
        $.ajax({
          type: "POST", 
          url: "administrador.php",
          data: {selectComponentes: dados}
        }).done(function (data) {
            console.log(data);
            //$("#modalVisualizarComponentes").show();
        });
    return false;
    }); 

// THAT’S THE INFORMATION I NEED TO CONSULT

selectComponentes=9

1 answer

3


it is possible to correct the error with a safecheck in your php:

if ($_POST['selectComponentes']) {
  //.. faz a consulta
}

however, note that the ideal is that ajax asks for another file and not the same as he is.

  • I completely changed the structure of the system to solve the problem, however I believe that with your code would have worked, so I will mark the question as answered, to help other people.

Browser other questions tagged

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