Another solution is still to use the element button
:
<button type="submit" name="plano" value="">Label</button>
When you set the type to submit
and have the value
attribute set, a key / value pair is generated in the information sent by the form when submitting it, just as with the input
element. In this way, it would be possible to have the elements:
<button type="submit" name="plano" value="basico">Escolha o plano básico</button>
<button type="submit" name="plano" value="medio">Escolha o plano médio</button>
<button type="submit" name="plano" value="avancado">Escolha o plano avançado</button>
In PHP, to recover this value, you would only need to use $_POST
or $_GET
, depending on the method used by the form:
<?php
switch ($_POST["plano"]) {
case "basico":
echo "Você escolhe o plano básico", PHP_EOL;
break;
case "medio":
echo "Você escolhe o plano médio", PHP_EOL;
break;
case "avancado":
echo "Você escolhe o plano avançado", PHP_EOL;
break;
}
See the W3C Element Specification for more details.
The main advantage of this method compared to the answer from Guilherme is that by using button
you can set the same value of attribute name
for all buttons, varying the value in value
. So, in PHP, you just need to validate on a value and not on three, as is using input
.
Even doing the above is not so good. Ideally, if the existing plans were stored in the database, associate the value of the button with the value of id
of the plane and not with the value of the name. Something like:
<button type="submit" name="plano" value="1">Escolha o plano básico</button>
<button type="submit" name="plano" value="2">Escolha o plano médio</button>
<button type="submit" name="plano" value="3">Escolha o plano avançado</button>
And in PHP just do:
$plano = $_POST["plano"];
Being able to save the value in the database directly.