how to send a result of a javascript function to php

0
function onQtyChange(e) {
  var row = this.parentNode.parentNode;
  var cellPrice = row.querySelector('td:nth-child(5)');
  var prevPrice = parseFloat(row.getAttribute('data-price'));
  var newQty = parseFloat(this.value);
  alert(prevPrice);
  var total = prevPrice * newQty;
  cellPrice.innerText = '$' + total;


}

this is what I want to do with the php

  $sumador_total=0;
  $total = ;
  $precio_total_f=number_format($total,2);//Precio total formateado
  $precio_total_r=str_replace(",","",$precio_total_f);//Reemplazo las comas
  $sumador_total+=$precio_total_r;//Sumador

  $subtotal=number_format($sumador_total,2,'.','');
  $total_iva=($subtotal * 12 )/100;
  $total_iva=number_format($total_iva,2,'.','');
  $total_factura=$subtotal+$total_iva;
    
asked by Kevin Jacho 01.06.2018 в 20:46
source

2 answers

0

// You can use Jquery's ajax

$.ajax({
    async: true,
    dataType: "json",
    type: "post",
    url: "miURL.php",
    data: {parametro1: "valor1", parametro2: "valor2"}
}).done(function (resp) {
    console.log("Servidor: ", resp);
}).fail(function (err) {
    console.log("Ocurrio un error", err)
}).always(function() {
    console.log("Ajax Terminado");
});

// From your php server

<?php

$parametro1 = $_POST["parametro1"];
$parametro2 = $_POST["parametro2"];

echo json_encode(Array("parametro" => $parametro1));

?>
    
answered by 01.06.2018 в 23:06
0

remember that javascript is a language that works on the client side, so the easiest way to save some parameter to php (works on the server side) is by means of:

  

ajax

making a request to the server to be executed

practical example:

$. ajax ({   method: "POST",   url: "some.php",   data: {name: "John", location: "Boston"} })   .done (function (msg) {     alert ("Data Saved:" + msg);   });

add api reference link: link

    
answered by 02.06.2018 в 17:40