Retrieve select input value

0

I have the following piece of code that allows me to store new information in a MySQL database and also allows me to recover the saved information and make a modification if I wish.

<input class="input_mayuscula" type="text" name="sistema_enc" value="<?php echo $alm->__GET('sistema_enc'); ?>" style="width:100%;" />

But I would like to simplify it with the use of a SELECT since the variables will always be the same. I have tried changing the original code by the SELECT and it works perfect to store new data, but when I am going to edit the option shown by default it is the one of position 0.

The SELECT code is:

<select name="sistema_enc" class="input_mayuscula" value="<?php echo $alm->__GET('sistema_enc'); ?>">
  <option value=""></option>
  <option value="MRW">MRW</option>
  <option value="DOMESA">DOMESA</option>
  <option value="GRUPOO ZOOM">GRUPO ZOOM</option>
  <option value="TEALCA">TEALCA</option>
  <option value="SEREX">SEREX</option>
</select>
    
asked by Jose M Herrera V 19.06.2018 в 12:39
source

2 answers

1

You have to check which matches the value you want to select in the option , not the select tag:

<select name="sistema_enc" class="input_mayuscula">
  <option value=""></option>
  <option value="MRW" <?php if ($alm->__GET('sistema_enc')=="MRW") {echo "selected";}?>>MRW</option>
  <option value="DOMESA"<?php if ($alm->__GET('sistema_enc')=="DOMESA") {echo "selected";}?>>DOMESA</option>
  <option value="GRUPOO ZOOM" <?php if ($alm->__GET('sistema_enc')=="GRUPOO ZOOM") {echo "selected";}?>>GRUPO ZOOM</option>
  <option value="TEALCA" <?php if ($alm->__GET('sistema_enc')=="TEALCA") {echo "selected";}?>>TEALCA</option>
  <option value="SEREX" <?php if ($alm->__GET('sistema_enc')=="SEREX") {echo "selected";}?>>SEREX</option>
</select> 

To be more effective in the code, you can save your options in an array and traverse it to create the options. In this way, if the options vary you only need to modify the array and not the code that shows the select .

<?php $opciones = array("MRW","DOMESA","GRUPOO ZOOM","TEALCA","SEREX");?>
<select name="sistema_enc" class="input_mayuscula">
  <option value=""></option>
  <?php for ($i=0; $i<count($opciones); $i++) {?>
    <option value="<?php echo $opciones[$i];?>" <?php if ($alm->__GET('sistema_enc')==$opciones[$i]) {echo "selected";}?>><?php echo $opciones[$i];?></option>
  <?php } ?>
</select>
    
answered by 19.06.2018 / 13:50
source
0

So that does not happen, you can determine which of the option is chosen by default with the property selected . For example:

<option value="MRW" selected>MRW</option>
<option value="DOMESA">DOMESA</option>

In this code the default option would be MRW

    
answered by 19.06.2018 в 13:23