Saving the value of the label on the bank with javascritp

Asked

Viewed 53 times

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.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

2 answers

1


According to your code, the "total" input text can receive a value that changes according to the type of delivery. The problem is that the value has "R$" in the beginning. If the database field accepts only numbers, it will not record this information, the result will be zero.

You can solve this by handling the total information before entering it into the bank. It would look like this:

<?php 
require_once("../conexao.php");
@session_start();
$total = $_POST['total'];
$total = str_replace("R$","",$total);
$total = str_replace(".","",$total);
$total = str_replace(",",".",$total);

res = $pdo->prepare("INSERT into vendas (total) values (:total)");
$res->bindValue(":total", $total);
$res->execute();
   
?>
  • Why put R$ "inside" the field... ? It would be better to have a field only with the value.

  • 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

  • I prefer $ to bring a nicer formatting @Bins

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.