php jquery get value from a div

0

I need to pass to letters (using a PHP script) a number that is inserted into a div by means of a calculation in Jquery. I need to put this value in a PHP variable. I can not do it. Any suggestions? Here the code:

<script type="text/javascript">
$(document).ready(function(){
//Subtotal	
	importe_total = 0
	$("#remitosfacturados .totalservicio").each(
		function(index, value) {
			importe_total = importe_total + eval($(this).val());
		}
	);
	var importe_totalEntero = importe_total; 
	var importe_total2Dec = importe_totalEntero.toFixed(2); 
	
	$("#remitosfacturados .subtotalfacturacion").html(importe_total2Dec);

//Calculo de IVA

var subtotal = 	$("#remitosfacturados .subtotalfacturacion").html();
var IVA = 	$("#remitosfacturados .porcentajeiva").val();

var resultadoIVA = (parseInt(subtotal) * (parseInt(IVA) / 100)).toFixed(2);

$("#remitosfacturados .iva_valor").html(resultadoIVA);


//TOTAL

var TotalFacturado = (parseInt(subtotal) + parseInt(resultadoIVA)).toFixed(2);

$("#remitosfacturados #totalfacturado").html(TotalFacturado);

});

</script>
<div class="form-title">
<div class="form-two widget-shadow">
<?php


$TotalFacturadoParaLetras = AQUÍ DEBERÍA IR EL VALOR DEL DIV #totalfacturado;
echo $TotalFacturadoParaLetras;
include ("valoresenletras.php");
	$totalenletras=(string)$TotalFacturadoParaLetras; 
	$V=new EnLetras(); 
$con_letra=strtoupper($V->ValorEnLetras($totalenletras,'pesos')); 
echo '<b>'.$con_letra.'</b>'; 

?>
</div>
</div>
    
asked by pointup 31.05.2018 в 20:29
source

1 answer

0

You have to use Ajax to send the value to the PHP script and put the answer wherever you want it on your page. Example:

<script type="text/javascript">
$(document).ready(function(){
//Subtotal  
    importe_total = 0
    $("#remitosfacturados .totalservicio").each(
        function(index, value) {
            importe_total = importe_total + eval($(this).val());
        }
    );
    var importe_totalEntero = importe_total; 
    var importe_total2Dec = importe_totalEntero.toFixed(2); 

    //Escribir importe en num y letra usando la función obtenerNombreDeCifra()
    $("#remitosfacturados .subtotalfacturacion").html((importe_total2Dec)
    + ' - ' 
    + obtenerNombreDeCifra(importe_total2Dec)); 

...

});

//funcion para enviar la cifra a PHP
function obtenerNombreDeCifra(cifra){
 let regreso;
 $.post( "ElScriptCorrecto.php", { cifra: cifra }, function(a){
   regreso = a;
 } );
 return regreso;
}
</script>

Then in your PHP script you use the POST variable:

<?php

//obtenemos el valor de POST
$TotalFacturadoParaLetras = $_POST['cifra'];

echo $TotalFacturadoParaLetras;
include ("valoresenletras.php");
...
?>

Note: To send values to PHP and receive processed values back, it is recommended to use JSON instead of HTML or plain text.

    
answered by 31.05.2018 в 21:37