How do I validate a value without leaving the page?

Asked

Viewed 125 times

2

I’m developing a system with the discount part, so I wanted the user to enter the discount and after he finished validating whether or not and show the discount percentage.

$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 on the same page the value of the discount, if you have, otherwise does not change anything.

  • what have you done ? post an example of your progress.

  • @Gabrielrodrigues The way I’m doing is passing to another page, the user enters the code and on the other page I do the checking. $cupom = $this->input->post('cupom_desconto');
 $desconto = $this->desconto_model->buscarDesconto($cupom, $atividade_codigo);

  • edit your question and post this information.

1 answer

1


A suggestion is to do by jQuery.

I made an example for you:

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));  
    }
});

Browser other questions tagged

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