How do I make each time the numbers get registered in the Database?

0

I made a counter similar to the "call of the next number" in a banking institution and wanted that when I called for example No. 1 that this number 1 was recorded in the database and when I called the number 2 that it also be recorded in DB, and so on ...

<!DOCTYPE html>
<html>
<head>
    <title>Contador</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/bootstrap.min.js"></script>
    <script type="text/javascript">


var Cont1 = 0;
function balcao1(){
Cont1 = Cont1 + 1;
document.getElementById('contador1').value = Cont1;
}


    </script>
<style type="text/css">
    #contador1{
        font:bold 150px Arial;
        padding: 50px;
        color:#FF0000;
        border:2px dashed #f1f1f1;
        width:350px;
        height:350px;
        text-align:center;
        line-height: 200px;
    }



    .btn{
        margin-top: 20px;
        margin-left:130px;
    }
</style>
</head>


<body>

        <div class="col-md-3">
    <center><h1><span class="label label-default">Senha A </span></h1></center>
    <input id="contador1"><br>
    <input type="button" class="btn btn-success" value="Pr&oacute;ximo" onclick="balcao1();">
        </div>



</body>
</html>
    
asked by anonymous 27.12.2017 / 17:17

1 answer

0
  

Saving the number of your code in a database using PHP:

You can use an AJAX request to access a PHP page and save the variable in the database, as implemented in your code:

<!DOCTYPE html>
<html>
<head>

    <title>Contador</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/bootstrap.min.js"></script>

<script type="text/javascript">

var Cont1 = 0;
function balcao1(){

Cont1 = Cont1 + 1;
document.getElementById('contador1').value = Cont1;

//AQUI A REQUISIÇÃO AJAX

    $.ajax({
        //pode ser post ou get
        type: "GET",

        //url aonde a pagina php responsavel por salvar no banco está.
        url: "registra_banco.php?valor=Cont1",

        //você está passando o valor de Cont1 via get para a pagina registra_banco.php

        cache: false,

        success: function (retorno) {
            alert("salvo no banco de dados!);
        },
        error: function(retorno) {
            alert("erro!);
        }
    });

}
</script>

<style type="text/css">

    #contador1{
        font:bold 150px Arial;
        padding: 50px;
        color:#FF0000;
        border:2px dashed #f1f1f1;
        width:350px;
        height:350px;
        text-align:center;
        line-height: 200px;
    }

    .btn{
        margin-top: 20px;
        margin-left:130px;
    }

</style>
</head>

<body>

<div class="col-md-3">
  <center><h1><span class="label label-default">Senha A </span></h1></center>
  <input id="contador1"><br>
  <input type="button" class="btn btn-success" value="Pr&oacute;ximo" onclick="balcao1();">
</div>

</body>
</html>
  

Good to remember that ajax is from the jquery library. it is sending the value to the page register_banco.php via get (by the url) and so later to be saved in the database

Example of how the page regis_banco.php:

<?php

//abre uma conexão com o banco de dados, esses parametros variam de como foi feito seu banco de dados
$conn = new PDO("mysql:host=localhost;dbname=data_dashboard", "root", "" );

//pega o valor passado via ajax e atribui a uma variavel 

if (isset($_GET['valor'])) {
  $valor= $_GET['valor'];
}

//agora que você tem o valor você precisa dar um insert no seu banco de dados
//monta a query que será executada no banco (depende de como está sua tabela tambem) 
$query = "insert into valores values('$valor')";

//rode o metodo de executar a sua query no banco 
$db->execute($query);

?>

If your screen displays the saved alert, it is because the ajax request worked out, if given error, it is because the ajax request failed

  

Remembering that there are many ways to do this in this method   POO and AJAX. were used to delve into the connection class with   the php database:

link

and for ajax I recommend:

link

    
27.12.2017 / 18:20