How to insert the button pressed in the database

Asked

Viewed 177 times

3

In my code I have 3 buttons, which serve for the user to classify 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 classify the service? The data is being entered without problem, I’m just having problems in how to insert in the field classificacao.

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');
    }
}

1 answer

2


Setting the color in value from 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">

In the PHP just do it like that:

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 button:

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
  • 1

    Thank you so much! I just hadn’t put anything in value because I don’t want any text to appear, but that’s the minimum! Thank you, it worked!

  • You can tbm make a checker on PHP for this case of not wanting text to appear, if you want I can edit with a way to make this check

  • That would be great of you, Wees!

  • Ready @Ana, I edited the answer

  • 1

    Thanks Wees, I tried without for anything on value however did not give, it only works if there is something inside value

  • 1

    I solved it with css, now everything is great, thanks again!

Show 1 more comment

Browser other questions tagged

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