Another solution is to use the element button
:
<button type="submit" name="plano" value="">Label</button>
When defined as type submit
and possess the attribute value
defined, a key/value pair is generated in the information sent by the form when submitting it, as well as with the element input
. 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, just 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 specification of the element for more details.
The main advantage of this method in relation to the William is that using button
it is possible to set the same value as the attribute name
for all buttons, varying the value in value
. This way, in PHP, just do the validation in one value and not in three, as is using the input
.
Even doing the above is not so indicated. Ideally, if the existing plans were stored in the database, associate the value of the button with the value of the id
of the plan 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"];
Can save the value in the database directly.
Got any try? , you entered the tag
PHP
, but I’m not seeing codePHP
none.– Matheus Borjes