Change select option to URL

Asked

Viewed 153 times

1

I have a select with five option. I want it to check the link the user is and as this link, it selects an option.

My HTML:

<select name="segmento" id="segmento" class="basic">
  <option value="">Segmento</option>
  <option value="Teste Engenharia">Teste Engenharia</option>
  <option value="Teste Hospitalar">Teste Hospitalar</option>
  <option value="Teste Ilumina&ccedil;&atilde;o">Teste Ilumina&ccedil;&atilde;o</option>
  <option value="Teste Elevadores">Teste Elevadores</option>
</select>

I’m trying to do via PHP with preg_match, but my knowledge is limited, so I’m probably doing something wrong.

What I’m trying to do:

<option
<?php 
if (preg_match( '/segmentos\/teste-hospitalar/',$database->parametros['menuRoteador']))
    echo 'value="teste Hospitalar" Teste Hospitalar';
elseif (preg_match('/segmentos\/neomot-elevadores/',$database->parametros['menuRoteador']))
    echo 'value="Teste Elevadores" Teste Elevadores';
elseif (preg_match('/segmentos\/neomot-engenharia/',$database->parametros['menuRoteador']))
    echo 'value="Teste Engenharia" Teste Engenharia';
?>
>
</option>

He should be occupied with the value of <option value="">Segmento</option>, but this is not happening.

1 answer

3


You can improve your logic, but I believe your mistake is just echos, for you are forgetting the character > tag option.

For example this echo:

value="teste Hospitalar" Teste Hospitalar

Should be like this:

value="teste Hospitalar">Teste Hospitalar

An alternative to simplify the visualization of the code is thus:

<?php 
if (preg_match( '/segmentos\/teste-hospitalar/',$database->parametros['menuRoteador']))
    $valor = 'teste Hospitalar';
elseif (preg_match('/segmentos\/neomot-elevadores/',$database->parametros['menuRoteador']))
    $valor = 'Teste Elevadores';
elseif (preg_match('/segmentos\/neomot-engenharia/',$database->parametros['menuRoteador']))
    $valor = 'Teste Engenharia';
?>
<option value="<?= $valor ?>"><?= $valor ?></option>
  • But the option is closed at the end of PHP

  • @Felipestoker Yes, you’re closing the option, but is forgetting to close the tag of beginning of option. See how it’s getting <option value="teste Hospitalar" teste Hospitalar></option>. To the browser the option has 3 attributes: value, teste and Hospitalar.

  • 1

    Ahh understood. You’re right. The problem is that in select it was like this Teste Elevadores>, was left a > afterward.

  • @Felipestoker There’s one left > after and missed one before =)

  • actually I had already made the modification you spoke about, look here as it is http://jsfiddle.net/o3y7yj3t/

  • worked :D just found it strange that the closing of the option is green http://prntscr.com/61cinw

  • Change your logic, remove all php code from within the option and assign the value to a variable, then select vc puts this variable in the value and display text. Will make the code more readable.

  • 1

    @Felipestoker It will get weird even in your IDE because it won’t be able to identify the closing of the option start tag. I added an alternative to the answer so that your IDE correctly identifies the code and makes it easier to interpret it.

  • Perfect, I understand your explanation! :)

Show 4 more comments

Browser other questions tagged

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