Get value for select JS/PHP

Asked

Viewed 41 times

0

I’m in doubt.

I have a questionnaire where the user will evaluate some sectors. I have for example:

 <label>Higiene</label>
<select class="form-control" required >
   <option value=""></option>
   <option value="1">1</option>
   <option value="2">2</option>
</select>

<label>Organização</label>
<select class="form-control" required >
   <option value=""></option>
   <option value="1">1</option>
   <option value="2">2</option>
</select>

<label>Ambiente</label>
<select class="form-control" required >
   <option value=""></option>
   <option value="1">1</option>
   <option value="2">2</option>
</select>

I know I can take the value of select when using the name, but what I want to do is also take the word that is on the label and be able to reference and then save in the bank, so I can record in the bank the note that the user gave and to which sector he gave. For example:

SECTOR ---- NOTE -----

Hygiene 2

Organization 1

Environment 0

  • 1

    But by naming, for example, the first <select> as name='higiene' you no longer know that this assessment is related to hygiene?

  • But when sending via POST, for example $_POST['hygiene'] I would only take the value of select, right? I need to somehow put Hygiene in some variable to be able to record in the bank, my form has several selects, I wanted to do it in a more dynamic way.

  • $_POST is an array, you can do the foreach...

1 answer

0


<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> 
<label for="higiene">Higiene</label>
<select name="higiene" id="higiene" class="form-control" required >
   <option value=""></option>
   <option value="1">1</option>
   <option value="2">2</option>
</select>

<label for="organizacao">Organização</label>
<select name="organizacao" id="organizacao" class="form-control" required >
   <option value=""></option>
   <option value="1">1</option>
   <option value="2">2</option>
</select>

<label for="ambiente">Ambiente</label>
<select name="ambiente" id="ambiente" class="form-control" required >
   <option value=""></option>
   <option value="1">1</option>
   <option value="2">2</option>
</select>

<input type="submit">
</form>

Use the foreach and switch syntax. See the 2 examples below:

<?php

  if (!empty($_POST)) {

    foreach ($_POST as $key => $value)
        {
    
            
            switch ($key) {
                case 'organizacao':
                    echo "Organização :". $value;
                    break;
                case 'ambiente':
                    echo "Ambiente :". $value;
                    break;
                case 'higiene':
                    echo "Higiene :". $value;
                    break;
            }           
        }
  }

See also in this example, concatenating inside the switch:

  if (!empty($_POST)) {

    foreach ($_POST as $key => $value)
        {
            switch ($key) {
                case 'organizacao':
                    $bd .= "'SETOR'=Organização, 'NOTA'=$value ...";
                    
                    break;
                case 'ambiente':
                    
                    $bd .= "'SETOR'=Ambiente, 'NOTA'=$value ...";
                    break;
                case 'higiene':
                    
                    $bd = "'SETOR'=Higiene, 'NOTA'=$value ...";
                    break;
            }   
            
        }
  }
  
  echo $bd;

The "result" of print(echo), will be: 'SETOR'=Higiene, 'NOTA'=1 ...'SETOR'=Organização, 'NOTA'=2 ...'SETOR'=Ambiente, 'NOTA'=1 ...

  • Thank you Fabiano!

Browser other questions tagged

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