pass values by ajax error

2

I have a routine to update the status and balance of a document, this is the jquery code

$("#invBtnPay").on("click", function (event) {
        alertify.confirm("Pagar Factura", "El Saldo de la Factura es de $" + $("#idt_saldo").text(),
            function () {
                var invoiceStatus = {
                    'inv_id': $("#inv_id").val(),
                    'inv_status': "Pagada",
                    'inv_pay': $("#idt_saldo").text(),
                };
                $.ajax({
                    type: 'POST',
                    url: './core/invoiceStatus.php',
                    data: invoiceStatus,
                    dataType: 'json',
                    encode: true
                })
                    .done(function (invoiceStatus) {
                        if (invoiceStatus.success) {
                            $('#inv_status').addClass('badge badge-warning');
                            $('#inv_status').text(invoiceStatus.status);
                            window.location.href = "account.php?page=adm_add_invoice&title=Facturacion";
                        }
                        else {
                            alertify.error(invoiceStatus.message);
                        }
                    });
            },
            function () {
                alertify.warning('Accion Cancelada!');
            }).set('labels', { ok: 'Hacer el Pago', cancel: 'Cancelar' });

and this is the php code

<?php
    require_once("../config.php");

    $inv_id = $_POST['inv_id'];
    $inv_status = $_POST['inv_status'];
    $inv_pay = (!isset($_POST['inv_pay']) ? 0 : $_POST['inv_pay']); //toma el valor y lo divide entre 1000

    $update = "UPDATE doc_master SET balance = balance - ?, status = ? WHERE id = ? LIMIT 1"; 
    $stmt = $conn->prepare($update);

    $stmt->bindParam(1, $inv_pay, PDO::PARAM_STR);
    $stmt->bindParam(2, $inv_status, PDO::PARAM_STR);
    $stmt->bindParam(3, $inv_id, PDO::PARAM_INT);

    $stmt->execute();

    $invoiceStatus = []; 

    if($update){
        $invoiceStatus ['success'] = true;
        $invoiceStatus ['invoiceId'] = $inv_id;
        $invoiceStatus ['message'] = "Factura ".$inv_status." con Exito!";
        $invoiceStatus ['status'] = $inv_status;
        echo json_encode($invoiceStatus);
    }
    else
    {
        $invoiceStatus ['success'] = false;
        $invoiceStatus ['message'] = "Algo salio mal, No fue posible actualizar en estado de la Factura!";
        echo json_encode($invoiceStatus);
    }
?>

As you can see, I send the id of the document ($inv_id) , the new state ($inv_status) and the value to discount ($inv_pay) and this is where the problem is, it happens that the value is sent 5,300.00 and php arrives 5.3 and this is a value that deducts from the total of the document in such a way that the balance remains at 5,294.70

in the image you see how the values are after the payment

How can I solve this problem?

Thanks for your help.

    
asked by Juan Carlos 09.04.2018 в 15:14
source

2 answers

0

What you receive on the server is a string and you can convert this value to a float and then perform the subtraction. To make the conversion you have the function floatval . On the documentation page you have another function to consider the thousands separator (among other things):

function tofloat($num) {
    $dotPos = strrpos($num, '.');
    $commaPos = strrpos($num, ',');
    $sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos : 
        ((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);

    if (!$sep) {
        return floatval(preg_replace("/[^0-9]/", "", $num));
    } 

    return floatval(
        preg_replace("/[^0-9]/", "", substr($num, 0, $sep)) . '.' .
        preg_replace("/[^0-9]/", "", substr($num, $sep+1, strlen($num)))
    );
}

$num = '1.999,369€';
var_dump(tofloat($num)); // float(1999.369)
$otherNum = '126,564,789.33 m²';
var_dump(tofloat($otherNum)); // float(126564789.33)
////Con tus valores:
$num='5,325.23';
var_dump(tofloat($num)); // float(5325.23)

Demo (from the documentation page that I left you):

Then you can take that value and subtract it from the database.

    
answered by 09.04.2018 / 17:26
source
0

Php reads the value 5,300 as: Five point 3 , not Five thousand three hundred. This is because you are sending the decimal point after five. If you want it to work you will have to remove the separator so php interprets it as five thousand three hundred.

Instead of sending: 5,300.00 . You must send 5300.00 .

Try this:

//..

var invoiceStatus = {
 'inv_id': $("#inv_id").val(),
 'inv_status': "Pagada",
 'inv_pay': $("#idt_saldo").text().replace(',',''), //<-- eliminas el separador , del monto enviado
};

//...
    
answered by 09.04.2018 в 16:02