How do I create a label of a given name within a <option> in a <select>?

Asked

Viewed 40 times

2

I’m looking to make a legend within an attribute <option> create a label with the name of a product, how do I properly display this label?

The error is in the line inside the attribute title="":

echo "<option title='{$v1['cd_produto']}'>{$v1['nome']}' value='{$v1['cd_produto']}'>{$v1['nome']}</option>";

What returns to me:

inserir a descrição da imagem aqui

Complete code:

<p> ID produto:
    <select onclick="buscaDados()" name="cd_produto" id="cd_produto" required="" title="Escolha a roupa">
        <option value="" title="Por padrão a opção é vazia, escolha abaixo o produto desejado"> Nenhum </option>
            <?php
                foreach ($resultado_produto as $v1) {
                    echo "<option title='{$v1['cd_produto']}'>{$v1['nome']}' value='{$v1['cd_produto']}'>{$v1['nome']}</option>";
                }
            ?>
    </select>
</p>
  • Cara has how to put in question a print of the final look that should be on the screen?

  • I’ll print it out so you can see what comes back.

  • @Xinante if the answer below has solved your problem, mark it as an accepted answer so you can help others as well. If not, please feel free to comment

1 answer

0


It is possible yes as the example below:

<select>
<option title="testando 1">teste 1</option>
<option title="testando 2">teste 2</option>
<option title="testando 3">teste 3</option>
</select>

What’s wrong with your code is with the quotation marks.

Try to make your code simpler, as follows:

<?php foreach($resultado_produto as $v1): ?>
    <option title="<?= $v1['cd_produto'] ?>" value="<?= $v1['cd_produto'] ?>"><?= $v1['nome'] ?></option>
<?php endforeach ?>
  • Solved no face, it seems that does not return the select with the keys.

  • @Xinante the code I created below returns as value in the form the field $v1['cd_produto']. So that when the form is sent, the attribute "name" in the select

  • There’s no way to redo it, it doesn’t work yet.

  • @Xinante what is not working explain mehor

  • Sorry man, the code works I had typed wrong, but now it works.

Browser other questions tagged

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