With PHP you can check which value is filled and use it. Like the other values will be strings empty, just make a logical operation between them:
$quantidade = $_POST['qtdbuffet1']
|| $_POST['qtdbuffet2']
|| ...
|| $_POST['qtdbuffet8'];
Thus, regardless of which value has been filled in, it will be attributed to $quantidade
. Can do:
$corpoMSG = "<strong>Quantidade Buffet:</strong> {$quantidade}<br>";
But there is no point in owning 8 fields to send only 1 value. See below how you can work around this using Javascript.
This is what we call the XY problem. You have an interface problem, in HTML, but you want to solve it in PHP. You can, but it’s going to be funny.
As you put it, you have 8 choice options on the screen for the user and as the choice will be limited the amount values. You have set 8 different fields, configuring as needed and it seems that you only display one as per user selection. As a consequence you will have 8 values in PHP and only one has been filled. By the HTTP message you only want to send one value: quantity. It doesn’t matter where it came from. Having 8 fields to send 1 value doesn’t make any sense.
If you need different validation rules according to the selected option, use Javascript:
const loja = document.querySelector('#lojaInput');
const quantidade = document.querySelector('#quantidadeInput');
loja.addEventListener('change', function () {
quantidade.max = loja.options[loja.selectedIndex].dataset.max;
});
input:invalid {
background-color: pink;
border: 2px solid red;
}
<form>
<select name="loja" id="lojaInput">
<option value="1" data-max="10">Loja 1</option>
<option value="2" data-max="20">Loja 2</option>
<option value="3" data-max="30">Loja 3</option>
</select>
<label>
Quantidade:
<input id="quantidadeInput" type="number" name="quantidade" max="10" value="0">
</label>
<button type="submit">Enviar</button>
</form>
Realize the value of max
country quantidade
is defined by the property data-max
of select
. If selected store 1 the maximum will be 10; store 2 will be 20 and store 3 will be 30.
In PHP, just take the value $_POST['quantidade']
. To validate the value also in backend, use the value of $_POST['loja']
as a reference.
Try naming (attribute
name
) all fields with the same value, but add a pair of square brackets at the end (ex:qtdbuffet[]
). After processing,$_POST['qtdbuffet']
will be an array with as many values asinput
are completed.– Bruno Augusto
beauty, I’m gonna test!
– Jvs Corrêa
I think the ideal would be to better explain what the field would be
qtdbuffetX
maybe the problem is the type of input that Voce is using, post the form snippet also helps.– Neuber Oliveira
Why you have 8 fields if only one will be filled?
– Woss
@Brunoaugusto I did what you said, but it brings me as a result "Array" instead of the number typed in the input
– Jvs Corrêa
@Andersoncarloswoss I have conditions that depending on a selected option it shows a type of input, so I have 8 fields
– Jvs Corrêa
And can’t it be the same? I can’t see the logic of 8 different.
– Woss
@Andersoncarloswoss I have 8 stores, each store has a maximum capacity, when I select store 1, it opens the input with a maximum number of guests, the input is configured for the user to enter the maximum limit of that unit that was selected, if it changes the unit again another type of input with maximum capacity is displayed
– Jvs Corrêa
This does not prevent you from having only one field, just make the appropriate settings with Javascript. If you want to send only one value, it makes no sense to have 8 fields. My opinion.
– Woss
@Jvscorrêa Most likely you are trying to use directly
$_POST['qtdbuffet']
in the same way as before (e.g., withecho
/print
). With this change in HTML the variable becomes an array (Doh) and therefore needs to be manipulated as an array, either by iterating or accessing its indices. An array, when manipulated as a string, will result in the wordArray
even.– Bruno Augusto