Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Hide id while show data from database in select option

enter image description here

Look at picture above. "2" is an id of "fsdfs". I need the id to process it in backend. But in frontend, i don’t want to show the id in that select option. Is there any way to make it real?

Here’s my currect code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div class="col-xl-6 col-md-6 col-12 mb-1">
    <div class="form-group">
        <label for="supplier_id">Supplier</label>
        <select class="form-control" name="supplier_id">
            <?php
                $sql = mysqli_query($con, "SELECT * FROM supplier ORDER BY id ASC");
                while($supplier = mysqli_fetch_array($sql)){
             ?>
            <option value="<?php echo $supplier['id'].' - '.$supplier['name']; ?>"><?php echo $supplier['id'].' - '.$supplier['name']; ?></option>
            <?php } ?>
        </select>
    </div>
</div>

>Solution :

Use $supplier['id'] as value (which will be sent when submitting the form) and $supplier['name'] as the text content of your <option>:

<div class="col-xl-6 col-md-6 col-12 mb-1">
    <div class="form-group">
        <label for="supplier_id">Supplier</label>
        <select class="form-control" name="supplier_id" id="supplier_id">
            <?php
                $sql = mysqli_query($con, "SELECT * FROM supplier ORDER BY id ASC");
                while($supplier = mysqli_fetch_array($sql)){
             ?>
            <option value="<?php echo $supplier['id']; ?>"><?php echo $supplier['name']; ?></option>
            <?php } ?>
        </select>
    </div>
</div>

More on <option>: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option

Sidenote: the for attribute of a <label> element should point to the id attribute of the related <input>. I added id="supplier_id" to your <select> to make it valid.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading