Insert button value into database

Asked

Viewed 43 times

1

In my project I am creating buttons (in exchange for a form) with the name of the tasks to register daily, so it becomes more practical and faster to register.

Code with the buttons:

<div class="form-group">
    <label class="col-md-4 control-label" for="PequenoAlmoco" id="acao"></label>
<div class="col-md-4">
        <button id="PequenoAlmoco" name="PequenoAlmoco" class="btn btn-info">Pequeno Almoço</button>
</div>
<div class="form-group">
    <label class="col-md-4 control-label" for="Almoco" id="acao"></label>
<div class="col-md-4">
        <button id="Almoco" name="Almoco" class="btn btn-info">Almoço</button>
</div>
<div class="form-group">
    <label class="col-md-4 control-label" for="Lanche" id="acao"></label>
<div class="col-md-4">
        <button id="Lanche" name="Lanche" class="btn btn-info">Lanche</button>
</div>
<div class="form-group">
    <label class="col-md-4 control-label" for="Jantar" id="acao"></label>
<div class="col-md-4">
        <button id="Jantar" name="Jantar" class="btn btn-info">Jantar</button>
</div>
</div>

My problem is how I save tasks that the user clicks on variables and do the insert in the database table.

  • Your question seems too wide, I think you need more of a tutorial than a more specific help. Have you looked at the other questions about entering data with php right here on the site? Like this: https://answall.com/questions/76766/comort-insert-com-valores-postados-em-textarea and https://answall.com/questions/248757/comort-valor-no-mysql-com-php-e-ajax

2 answers

2


For this you need to use Ajax and php let’s go I will create a simple example for Voce better understand:

<button type="button" id="pequeno-almoco">Pequeno Almoço</button> <!-- Botão para executar a ação -->

<div id="resposta"></div> <!-- Aqui sera exibida a resposta da ação fica a critério  -->

    <script type="text/javascript">
    $(document).ready(function(e) {

         $(document).on("click","#pequeno-almoco",function(e) { //Apos clicar no botão ativa o evento onclick

            var p_almoco = $(this).attr('id'); //Variavel de onde recebe o valor do id do botão 

            //Ajax
            $.ajax({
                url: 'almoco.php', //Url para onde sera passado o parametro via POST
                type: 'POST',      
                data: {p_almoco : p_almoco}, //Passando o valor da variavel para o post     
                cache: false,
                success: function(returnhtml) {                          
                    $("#resposta").html(returnhtml); //Resposta da ação 
                }           
            });    
            //
        });

    });
    //
    </script>

In your php almoco.php

$valor = $_POST['p_almoco'];

With this variable received by the post update the database, do the same for the other buttons.

1

From what I understand, you can put in a <form> and those buttons are a input of the kind Submit. It would look something like this:

<form method="POST"> 
<div class="form-group">
<label class="col-md-4 control-label" for="PequenoAlmoco" id="acao"></label>
<div class="col-md-4">
    <input type="submit" id="PequenoAlmoco" name="tarefa" value="PequenoAlmoco" class="btn btn-info">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="PequenoAlmoco" id="acao"></label>
<div class="col-md-4">
    <input type="submit" id="Almoco" name="tarefa" value="Almoco" class="btn btn-info">
</div>
</div>
</form>

Then just take the value in the $_POST['task']. I believe this makes it easier

Browser other questions tagged

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