I am putting together a system to calculate freight in php, ajax and javascript. The calculates-frete page receives a value through the number_format(($_SESSION['total']), 2, ',', '.')
that comes from the shopping cart.
After calculating the freight, I present the total value of the purchase (with freight). But the sum does not beat. A purchase in the amount of 1,575.30 with 17.20 freight gives a total of 18,775. I believe there is a conflict between number_format
and parseFloat
or replace
of javascript.
Follow the html and javascript. If anyone can help me, thank you in advance.
<input id ="valor_produto" type ="text" value = "<?php echo number_format(($_SESSION['total']), 2, ',', '.'); ?>"/><br/><br/>
Cep Destino = <input id ="cep_destino" type ="text" value ="", maxlength ="8"/><br/><br/>
Valor Frete = <input id ="valor_frete" type ="text" value =""/><br/><br/>
Valor Total = <input id ="valor_total" type ="text" value =""/><br/><br/>
<button type="button" onclick ="LoadFrete();">Calcular Frete</button>
function LoadFrete(){
var cep_destino = $('#cep_destino').val();
var valor_produto = $('#valor_produto').val();
$.ajax({
url: 'ajax/a_frete.php',
type: 'POST',
dataType: 'html',
cache: false,
data: {cep_destino: cep_destino},
success: function (data){
console.log(data);
$('#valor_frete').val(data);
var valor_produto = $('#valor_produto').val();
var total = parseFloat(data) + parseFloat(valor_produto);
$('#valor_total').val(total);
$('#prazo_entrega').val(data);
}, beforeSend: function (){
}, error: function (jqXHR, textStatus, errorThrown) {
console.log('Erro');
}
});
}