How do I validate a value without leaving the page?

2

I'm developing a system with the part of the discount, so I wanted the user to type the discount and after that it would validate if it has or not and the percentage of the discount appears.

$cupom = $this->input->post('cupom_desconto');
$desconto = $this->desconto_model->buscarDesconto($cupom, $atividade_codigo);

This is the way I am doing, but this way is sending the request to another page and I would like it to be done on the same page, example of Netshoes and others that we put the code and if it is valid appears in the same page the value of discount, if it has, otherwise it does not change anything.

    
asked by anonymous 09.11.2015 / 20:12

1 answer

1

One suggestion is to do for jQuery.

I gave you an example:

Fiddle

Basically:

$("#valorPorcentagem").change(function(e, j){
    var valorDigitado = this.value;
    var valorProduto = 300.00;

    if (valorDigitado == "") {
        alert("Sem desconto!")   
    } else {
        alert("O produto terá um desconto de: " + valorDigitado + "%. Valor final do produto: R$" + ( ((100-valorDigitado)/100) * valorProduto));  
    }
});
    
09.11.2015 / 20:57