How to insert the loaded button into the database

3

In my code I have 3 buttons, which serve for the user to sort the service:

<input type="submit" name="verde" class="verde" value="">
<input type="submit" name="amarelo" class="amarelo" value="">
<input type="submit" name="vermelho" class="vermelho" value="">

How do I put in the database, in the table, the "button" that he chose to sort the service? The data is being inserted without problem, I'm just having problems with how to insert into the classificacao field.

So far I'm doing this:

if (isset($_POST['verde']) || isset($_POST['amarelo']) || isset($_POST['vermelho'])) {
    $inserir=mysqli_query($link,"INSERT INTO questionario (pergunta_id, pergunta, hora, idioma, origem, classificacao) VALUES ('','$stringarray','$hora','$lang', '','')");
    if (!$inserir) {
        echo "Erro ao inserir na tabela.";
    } else {
        header('location: comentario.php');
    }
}
    
asked by anonymous 05.06.2018 / 14:02

1 answer

2

Defining the color in the value of the button instead of name :

<input type="submit" name="botao" class="verde" value="verde">
<input type="submit" name="botao" class="amarelo" value="amarelo">
<input type="submit" name="botao" class="vermelho" value="vermelho">

No PHP does so just:

if (isset($_POST['botao'])) {
    $classificacao = $_POST['botao'];
    $inserir=mysqli_query($link,"INSERT INTO questionario (pergunta_id, pergunta, hora, idioma, origem, classificacao) VALUES ('','$stringarray','$hora','$lang', '','$classificacao')");
    //restante do código

Edit with the check, without having to fill the value of the buttons:

if (isset($_POST['verde'])){
    $classificacao = $_POST['verde'];
}elseif(isset($_POST['amarelo'])){
    $classificacao = $_POST['amarelo'];
}elseif(isset($_POST['vermelho'])){
    $classificacao = $_POST['vermelho'];
}
if(isset($classificacao)){
    $inserir=mysqli_query($link,"INSERT INTO questionario (pergunta_id, pergunta, hora, idioma, origem, classificacao) VALUES ('','$stringarray','$hora','$lang', '','$classificacao')");
    //restante do código
    
05.06.2018 / 14:35