Calculations in PHP

Asked

Viewed 932 times

-2

I’m starting in PHP and have the following situation. I have a value, rescued from a bank, and six buttons with the following values: +100, +200, +300, +400, +500 e +600. How do I click on one of these buttons, is done the sum of the chosen value through the buttons with the value redeemed in bank?

Example: I click the button +500 and the value redeemed at the base is 1.000. The system sums up and saves the total.

  • 6

    Where exactly is your doubt? What have you tried to do?

2 answers

1

You’ll have to do something like this.

<?php      


            /*
                aqui esta a parte server-side do seu codigo esta será executada no servidor cada 
               vez que o usuário submeter a pagina.    
            */

            // abra sua conexão
        if(isset($_POST["acao"])&&$_POST["acao"] == "update"){// e verifique qual ação a se fazer
            $valor = $_POST["valor"];
            $update = mysql_query("UPDATE AlgumaTabela SET Valor = '".$valor."' WHERE id=X ");
        }

        // selecione o valor a resgatar do banco de dados      
        $select = mysql_query("SELECT Valor FROM AlgumaTabela WHERE id=X ");
        $valor = 0;
        if($row = mysql_fetch_assoc($select)){
            $valor = $row["valor"];
         }
?>    
<html>    
<head>
  <script>
  // função "send" recebe o elemento que você clicou soma o valor resgatado do banco e submete a pagina para o mesmo ser atualizado
  function send(elem){
   var mValue = parseInt(document.getElementById("valor").value);
   document.getElementById("valor").value = mValue+parseInt(elem.value);
    document.getElementById("myform").submit();    
  }
  </script>
</head>

<body>
  <form action="/" method="post" id="myform">
    <input type='hidden' name="acao" value="update"><!-- campo que contem a ação a ser tomada.-->
    <input type='hidden' name="valor" id="valor"  value="<?php print $valor ?>" ><!-- imprime o valor resgatado do banco para futuras atualizações --->

    <input type="button" onclick="send(this)" value="100">
      <input type="button" onclick="send(this)" value="200">
        <input type="button" onclick="send(this)" value="300">
          <input type="button" onclick="send(this)" value="400">
            <input type="button" onclick="send(this)" value="500">
              <input type="button" onclick="send(this)" value="600">


  </form>  
</body>
  • 2

    Vinicius, the cool thing is to explain the why (in this case, server-side and client-side)... that story of giving a fish or teaching to fish ;)

  • 2

    You are right @brasofilo I did in a hurry but I will detail :)

  • If possible @Tuyoshi already teaches by making use of PDO pq mysql_* is deprecated.

0

You redeem the value of the database and save it in a variable, add the value of the button to the value of the variable and update.

ao clicar no botão +500:
valor_botao = 500
valor_banco = query("consulta valor no bd")
valor_total = valor_banco + valor_botao
update("valor = valor_total")

If this does not answer your question, try to detail.

Browser other questions tagged

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