0
Personal I have this input that brings the total value of the purchase and I have this script that changes the value of the input according to the type of delivery, IE, if it is delivery, show total value + delivery fee and if it is withdrawn, show total value without delivery fee.
<!-- INPUT QUE TRAS O TOTAL VARIANDO COM O TIPO DE ENTREGA -->
<form method="post" class="mt-2">
<label class="text-dark" for="exampleInputEmail1">Total</label>
<input type="text" class="form-control form-control-sm" id="total" name="total">
<script>
var delivery = "R$<?php echo $sub_total; ?>";
var reitrada = "R$<?php echo $total; ?>";
var valor = delivery;
function addEventHandler(elem, eventType, handler) {
if (elem.addEventListener)
elem.addEventListener(eventType, handler, false);
else if (elem.attachEvent)
elem.attachEvent('on' + eventType, handler);
}
addEventHandler(document, 'DOMContentLoaded', function() {
addEventHandler(document.getElementById('tipoentrega'), 'change', function() {
if (document.getElementById("tipoentrega").value == 'Delivery') {
valor = delivery;
} else {
valor = reitrada;
}
console.log(valor);
document.getElementById('total').value = valor;
});
});
</script>
</form>
<!-- FIM DO CALCULO -->
I have this ajax that searches the fields on the finish.php page and inserts them into the database:
<!--AJAX PARA INSERÇÃO DOS DADOS -->
<script type="text/javascript">
$(document).ready(function() {
$('#btn-finalizar').click(function(event) {
event.preventDefault();
$.ajax({
url: "carrinho/finalizar.php",
method: "post",
data: $('form').serialize(),
dataType: "text",
success: function(mensagem) {
$('#mensagem').removeClass()
if (mensagem == 'Cadastrado com Sucesso!!') {
$('#mensagem').addClass('text-success');
alert('Pedido Finalizado!');
window.location = 'painel-cliente/index.php?acao=pedidos';
//$('#btn-fechar').click();
//location.reload();
} else if (mensagem == 'Mercado Pago!!') {
atualizarUltimaVenda();
$("#modal-mp").modal("show");
} else {
$('#mensagem').addClass('text-danger')
}
$('#mensagem').text(mensagem)
},
})
})
})
</script>
I will summarize the page to finish.php according to the data cited above:
<?php
require_once("../conexao.php");
@session_start();
$total = $_POST['total'];
res = $pdo->prepare("INSERT into vendas (total) values (:total)");
$res->bindValue(":total", $total);
$res->execute();
}
When I finish the purchase, the order is issued as a certificate, but the value of the total column is always 0.00, that is, the value of the input is not inserted but in the input itself it brings the correct calculation.
Why put R$ "inside" the field... ? It would be better to have a field only with the value.
– Peter
Our guy, how I love and hate the kkk programming was exactly that. The type of the total column in the bank is decimal(8,2). I withdrew the R $ and it worked perfectly. Thank you very much. Saved my life kkk
– user195714
I prefer $ to bring a nicer formatting @Bins
– user195714