Problem passing value 0 as AJAX parameter to PHP API.

0

I have the following jQuery block:

$("button[name='btnConfirmSalvarAltEstoque']").on('click', function(){
    var idEstoque = $(this).attr("id");
    var novaQuantidade = $("input[name='iptValorEstoqueEditar']").val();
    editarEstoque(idEstoque, novaQuantidade).then(res => {
        if(res == 'Ok')
        {
            $('#mdlEditarEstoque').modal('hide');
            toastr.success('Estoque mínimo editado com sucesso!');
            estoque();
        }
    }).catch(err => {
        $('#mdlEditarEstoque').modal('hide');
        trataErro(err, false);
    });
});

When the user clicks the btnConfirmSalvarAltEstoque button, I will call a function that expects 2 parameters, an id, and the new quantity, both of which are integer values. The function in turn is as follows:

function editarEstoqueMinimo(idEstoque, novaQuantidade){
    return new Promise((resolve, reject) => {
        $.ajax({
            type: "POST",
            url: "../api/api.estoque.php",
            data: {
                    "ajax-params": 4, 
                    "Id_Estoque": idEstoque,
                    "Num_QtdMinima": novaQuantidade
            },
            dataType: "JSON",
            success: function(res){
                resolve(res);
            },
            error: function(res){
                reject(res);
            }
        });
    });
}

The function above will make a AJAX request by calling the code function 4 and passing the 2 parameters. The code 4 function in the API is as follows:

case 4: //Editar Estoque
    $filters = [
        "Id_Estoque" => FILTER_VALIDATE_INT,
        "Num_QtdMinima" => FILTER_VALIDATE_INT
    ];

    $resultFilters = FILTER_INPUT_ARRAY(INPUT_POST, $filters);

    if(!in_array(FALSE, $resultFilters))
        echo editarEstoque(intval($_POST['Id_Estoque']), $_POST['Num_QtdMinima']);
    else
        echo parametroInvalido();
break;

The function above treats the values that came from the request to know if they are in fact integer values. If it is, call the editarEstoque function by passing the 2 parameters, the editarEstoque function is as follows:

function editarEstoque($Id_Estoque, $Num_QtdMinima)
{
    $estoque = new EstoqueController();
    $estoque->setId_Estoque(intval($Id_Estoque));
    $estoque->setNum_QtdMinima(intval($Num_QtdMinima));
    $retornoEstoque = $estoque->update();

    if($retornoEstoque)
        $retornoEditar = 'Ok';
    else
        $retornoEditar = null;

    return json_encode($retornoEditar);
}

The problem is when the value of the variable novaQuantidade in AJAX is 0 , the whole code is executed but it just does not work, if I pass any value other than 0 (1 or greater) the code works normally . I imagine that at some point, the value 0 is considered as null, not the same, then it interprets as if the variable had not come, how can I handle it?

The problem happens in the API in PHP, because if I test with Postman with a value greater than 0, it works normally, if I put 0, I get the problem.

    
asked by anonymous 23.09.2018 / 17:27

0 answers