perform a query inside javascript

1

I would like to know if a query can be made within javascript , I was investigating a bit and they said that opening php but I need to use the value of the query to perform calculations in javascript . How could he do it?

This is the script that I have inside the I need to make the query

$(document).on("keyup","#calibre,#medio",function(){
  var nom= $(this).data("nombre")
  var id= $(this).data("id_memoria")                
  var TAG_Conductor= $(this).text();
  alert(id);
  alert(TAG_Conductor);
  alert(nom);
})
    
asked by Daniel rodas garcia 26.04.2018 в 17:01
source

1 answer

3

For that you need to use AJAX.

HTML

<table>
  <tr>
    <td id='resultadoID'></td>
    <td id='resultadoTAG'></td>
    <td id='resultadoNOM'></td>
   </tr>
</table>

JS

$(document).on("keyup", "#calibre", function() {
    var nom = $(this).data("medio");
    var id = $(this).data("id_memoria"); 
    var TAG_Conductor = $(this).text();

    $.ajax({
      type: 'POST',
      url: 'buscadorDatos.php',
      data: {nom: nom, TAG_conductor:TAG_conductor, id:id },

      success: function(resultado) {
       $('#elementoDestino').html(resultado.split("#")[1]);
      },
      error: function(resultado) {
       console.log("Error buscarDatos: " + resultado);
       $('#observaciones').val("");
      }
    }) 

PHP

//recuperamos los valores pasados por POST desde AJAX
$id = $_POST['id'];
$tag = $_POST['TAG_conductor'];
$nom = $_POST['nom'];
//consulta SQL
$sql="select * from int_baja_tension where calibre='".$calibre."'"; 
$dato=$db->query($sql); 
if ($row=mysqli_fetch_array($dato)) { 
  $cal_uno=$row['310-17']; 
} 
$totcal1=$cal_uno*0.65;
//cuando tengas tus valores calculados de todo simplemente hacer un echo
echo "$valoresQueNecesitesEnJsSeparadosPor#";

With this you would be calling the file findData.php, and you would pass it through POST your variables collected in JS (id, nom, TAG_conductor).

In that PHP file you collect the values, process them as you need and simply make them an "echo" to show them in that PHP (although you or the user will not actually see it "painted" on screen)

As the JS function will capture what you have put in the "echo", what I propose is that the php file generate a string of the type "data1 # data2 # data3 ..." (in your case it would be something like for example "luis perez # tag luis perez # id luis perez") with the data you need to use later in JS separated by a #, so that when you capture them later in the success, you can divide them with the split function, as you see in the example.

So you would have values returned from php, which you could use in JS for your calculations.

If the php returns the string "luis perez # tag luis perez # id luis perez", with the function split you would have something like that.

resultado.split("#")[0] ---> luis perez
resultado.split("#")[1] ---> tag luis perez
resultado.split("#")[2] ---> id luis perez
    
answered by 26.04.2018 / 17:09
source