How to execute a code by clicking on a link?

Asked

Viewed 3,483 times

0

I am creating a site that will have a login system. When you log 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...

By clicking on some product, it will be selected, will have to take the product value, check if it reached its credit limit, if it hit, will have to show a message on the screen, if it did not hit, the user can select another product...

Example:

The user plan is R $ 30,00 real credit to spend.

The user chose a product with the value of: R$ 15,00. The system will take this value and check if reached the limit of his plan, if not hit, he can continue selecting the products, if he select another product, but the value of it is $ 20,00, there will show a message on the screen, saying that passed the limit.

How can I do that?

Could provide a functional example here?

  • 1

    The question is a little too wide. Could you make it more specific by saying where is your difficulty? Buying values? Treat click? Communicate with the server? The way it is, it looks like a code request that solves all of this at once, which is not suitable for the website format. Thank you.

  • A suggestion: when logging in, leave a session variable with the value of the user’s credit. In javascript, you control for each click on a product the value of credit.

3 answers

1

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

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

Remembering that you must create a function to take the balance and use it in the answer of the page.

Below answer to if the balance is not as 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";

}

If function:

PHP.NET

0

If you don’t want to leave the page you’re on, use an Ajax call. This way, new PHP code is executed without asking to load the page again.

I’ll give you a simple example that you can adapt. Here’s your product link:

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

Your link starts calling a javascript function. Inside this function has the call Ajax, as shown below. By Ajax is called the file seu_ficheiro_ajax.php, which will do the calculations you need in PHP. If you need to return something, do it with the command "echo" inside the Ajax file, this return will be in the javascript variable "html".

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, don’t forget to put inside the head of the page the jQuery include, for example:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
  • Tigernuno, for me to understand better about what you used, because I’m beginner in ajax, for example: What would be: (date:) and what’s inside it: (date: { parametro_1 : variavel_js1, parametro_2 : variavel_js2 }, ) That cache ? Can you explain line by line? Thank you!!

  • Of course, I explain what you have doubts. Do you know javascript? parametro_1 and parametro_2 are the parameters sent through POST, hence they are within date. Inside you can add more parameters to be sent if you need to. You put in parameter_1 a javascript variable with the value you need, variavel_js1. Then, in the Ajax file "seu_ficheiro_ajax.php", you will find these values using $_POST['parametro_1']. I hope I was clear. Any other questions, ask.

0

When the user selects the product, you can make an AJAX request to check if it has balance, based on the check you can (or not) show the alert.

Behold : Jquery Ajax

  • No <a href="#"> image </a> What event can I use there by clicking this link, make an ajax request? This request I can call a PHP file so I can check and do all the calculation there?

  • Use the click (jQuery): http://api.jquery.com/click/ Yes the intention is exactly this, php returns you if it has balance and you continue running the application.

  • Always prefer to put all possible information in the answer, so editing is allowed. Comments are not indexed by searchers and makes it difficult to help other people in the future that is our goal.

Browser other questions tagged

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