How to turn multiple variables into one PHP

Asked

Viewed 82 times

-2

I have a form with 8 input fields that take the entered value, I pick them by the POST but only 1 of the 8 fields is filled by the user, and in the POST I’m passing the 8 values even having only 1 filled, have as I turn the 8 variable of the post in one?

In case I’m doing it

$nbuffet1 = $_POST['qtdbuffet1'];
$nbuffet2 = $_POST['qtdbuffet2'];
$nbuffet3 = $_POST['qtdbuffet3'];
$nbuffet4 = $_POST['qtdbuffet4'];
$nbuffet5 = $_POST['qtdbuffet5'];
$nbuffet6 = $_POST['qtdbuffet6'];
$nbuffet7 = $_POST['qtdbuffet7'];
$nbuffet8 = $_POST['qtdbuffet8'];

$corpoMSG = " 

<strong>Quantidade Buffet:</strong> $nbuffet1 $nbuffet2 $nbuffet3 $nbuffet4 $nbuffet5 $nbuffet6 $nbuffet7 $nbuffet8<br>

 ";
  • 1

    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 as input are completed.

  • beauty, I’m gonna test!

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

  • Why you have 8 fields if only one will be filled?

  • @Brunoaugusto I did what you said, but it brings me as a result "Array" instead of the number typed in the input

  • @Andersoncarloswoss I have conditions that depending on a selected option it shows a type of input, so I have 8 fields

  • 1

    And can’t it be the same? I can’t see the logic of 8 different.

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

  • 1

    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.

  • @Jvscorrêa Most likely you are trying to use directly $_POST['qtdbuffet'] in the same way as before (e.g., with echo/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 word Array even.

Show 5 more comments

2 answers

3

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.

  • Now it was clear, I was following a complicated path, I hadn’t thought of it that way, thank you for guiding me!

  • [pt.so] is for that xD

0


You can concatenate with the operator . .

    $resultado = $nbuffet1 . ' ' . $nbuffet2 . ' ' . $nbuffet3 . ' ' . $nbuffet4 . ' ' . $nbuffet5 . ' ' . $nbuffet6 . ' ' . $nbuffet7 . ' ' . $nbuffet8;
$corpoMSG = " 

<strong>Quantidade Buffet:</strong> $resultado <br>

 ";

or use Heredocs:

$corpoMSG = <<<EOD
<strong>Quantidade Buffet:</strong>$nbuffet1 $nbuffet2 $nbuffet3 $nbuffet4 $nbuffet5 $nbuffet6 $nbuffet7 $nbuffet8<br>
EOD;
  • NOTE: If variables are numerical use strval to convert them to string.

  • 1

    Won’t this generate multiple white spaces in value? And what is the difference of these solutions to the one presented in the question itself?

  • 1

    @Andersoncarloswoss. That’s exactly what he wants the white spaces between the values.

  • 1

    "but only 1 of the 8 fields is filled in by the user", see question. Only one of the values is filled in. And if so, there was already the solution in the question itself.

  • 1

    So what’s the problem? The value for buffet will appear the way that guy wants it.

  • 1

    @Andersoncarloswoss and if you don’t want the spaces just be smart and remove them.

Show 1 more comment

Browser other questions tagged

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