Switch case on Submit button according to button ID

Asked

Viewed 499 times

0

I have 7 buttons of Submit in a form. Being:

Four of that:

<input type="submit" id="chamado1" class="mr-abre-btn" value="Abrir chamado">

Two of this:

<input type="submit" id="chamado2" class="mr-abre-btn" value="Encaminhar chamado">

And one of that:

<input type="submit" id="chamado3" class="mr-encerra-btn" value="Encerra chamado">

The problem is that I’m trying to make a switch case or an if in the value of a input hidden that I have. I’m doing it this way:

<input type="hidden" name="situacao_id" value="
<?php
    $situacao_chamado = $_POST['id'];
            switch ($situacao_chamado) {
                    case 'chamado1':
                        echo "1";
                        break;
                    case 'chamado2':
                        echo "3";
                        break;
                    case 'chamado3':
                        echo "3";
                        break;
                }
                ?>"

However, when I look for this variable:

$situacao_chamado = $_POST['id'];

Ends up generating the error below:

Notice: Undefined index: id in C:\wamp64\www\form\formulario.php on line 167 Call Stack #TimeMemoryFunctionLocation 10.0010242264{main}( )...\formulari.php:0 20.0111307088include( 'C:\wamp64\www\form\formulario.php' )...\formulario.php:44 "> 
  • 1

    What is the need for 7 buttons? Why are there repeated buttons? Know the attribute value id is not passed to PHP through the request. The only values will be the pair name/value. In doing $_POST["id"] you are trying to take the value of a field with name="id", that does not exist in the question. If this amount of buttons actually makes sense, set the attribute name and check it in PHP. Remember that the value will be value, nay id.

  • @Andersoncarloswoss, I need several input, because the form is dynamic, are yes and no questions and depending on the answer leads to another question, so he has in certain question the closure of the call or opening of the call. And Inputs appear in certain questions. It was the most objective way I could create. Thanks for the tip.

1 answer

1


This error occurs because the $_POST['id'] search for an input that has name="id", just tidy up your inputs by placing a name, so it will take the inputs that have the name="id" and give them the values determined by value=""

<input type="submit" name="id" id="chamado1" class="mr-abre-btn" value="Abrir chamado">

<input type="submit" name="id" id="chamado2" class="mr-abre-btn" value="Encaminhar chamado">

<input type="submit" name="id" id="chamado3" class="mr-encerra-btn" value="Encerra chamado">

In your PHP just change the case:

switch ($situacao_chamado) {
    case 'Abrir chamado':
        echo "1";
        break;
    case 'Encerrar chamado':
        echo "3";
        break;
    case 'Encaminhar chamado':
        echo "3";
        break;
}

This should solve your problem but I believe that the best would be to put radioButtons and only one input

  • thanks for the help, I solved my problem, I modified a little to my need. Thank you

Browser other questions tagged

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