How to execute a code when clicking on a link? [closed]

0

I am creating a website that will have a login system. By logging into the site, you will have several products inside, one next to the other ...

Each product will have its value, and each user will have a credit to spend on the site ...

When you click on a product, it will be selected, it will have to pick up the product value, check if it has reached its credit limit, if it has reached, it will have to show a message on the screen, if it did not hit the user you can select another product ...

Example:

The user's plan is $ 30.00 of credit to spend.

The user chose a product with the value of: R $ 15.00. The system will get this value and check if it has reached the limit of its plan, if it has not reached, it can continue selecting the products, if it selects another product, but the value of it is $ 20.00, there it will show a message in the screen, saying that it has crossed the line.

How can I do this?

Could you provide a working example here?

    
asked by anonymous 30.01.2014 / 11:30

3 answers

1

This will check if the balance is equal to the price of the product:

if ($precodoproduto == "'.$saldo.'") {
    --------- Resposta depois da escolha do produto -----------
    }

Remember that you should create a function to take the balance and use it in the page response.

Here is the answer if the balance is not necessary:

if ($precodoproduto == "'.$saldo.'" && $saldo != "'.$precodoproduto") {

$error = '    <SCRIPT LANGUAGE="JavaScript">
<!--
if (!confirm('Você não tem pontos//moedas suficientes para efetuar está compra'))
{
history.back();
}
// -->
</SCRIPT>';
echo "$error";

}

Function if:

  

PHP.NET

    
30.01.2014 / 12:33
0

When the user selects the product, you can make an AJAX request to verify that it has balance, based on the verification you can (or not) display the alert.

See: Jquery Ajax

    
30.01.2014 / 11:45
0

If you do not want to leave the page you are on, use an Ajax call. This way, you run new PHP code without asking to load the page again.

I'll give you a simple example that you can adapt. Here is the link to your product:

<a href="javascript:void(0)" onclick="verificar_saldo();">imagem</a>

Your link will call a javascript function. Within this function you have the Ajax call, as I'll show below. By Ajax is called the file your_file_ajax.php, which will do the calculations you need in PHP. If you need to return something, do it with the "echo" command inside the Ajax file, this return will be in the "html" javascript variable.

function verificar_saldo()
{

$.ajax({
    type: "POST",
    url: "seu_ficheiro_ajax.php",
    data:   {
                parametro_1 : variavel_js1,
                parametro_2 : variavel_js2
            },
    cache: false,
success: function(html)
    {
        // seu código javascript/jquery a ser executado com o retorno (html) da chamada Ajax

        if(html >= 20) alert("ERRO: passou o seu limite");
    }
});

}

Note

As you are using jQuery, do not forget to put the jQuery include inside the head of the page, for example:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    
30.01.2014 / 14:07