Hide id while show data from database in select option

Advertisements

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:

<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.

Leave a ReplyCancel reply