Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a problem regarding obtaining id from Combo Box populating name of customer from a database containing field id & name. I want to get the id of name selected by user from Combo Box. Following is the code i have applied to populating Combo Box:

PHP
<input type="text" class="input-field" id="registerNumber" name="register_number" 
               list="register_number" placeholder="Select Register No" /></label>

<!-- Populating Editable ComboBox -->
    <datalist id="register_number" >
    <?php                               
        $result_set= mysqli_query( $connection, "Select  id,  customer_name from 
              service_register ");

       while($result = mysqli_fetch_assoc($result_set)){
            echo "<option value=\"{$result["customer_name "]}\" 
                          id=\"{$result["id"]}\"></option>";
        }   
?>
Posted

There is no straight way to get datalist option text in javascript

You can add one more attribute id and put that text value there and get the result like below

HTML

HTML
<input list="emp_names" id="txt_name">
   <datalist id="emp_names">
     <option value="Mehta, Jack1" id="15">15</option>
     <option value="Mehta, Jack2" id="16">16</option>
     <option value="Mehta, Jack3" id="17">17</option>
     <option value="Mehta, Jack4" id="18">18</option>
     <option value="Mehta, Jack5" id="19">19</option>
   </datalist>
   <input type="button" onclick="alertTheSelectedValue()" value="submit">


JS

JavaScript
 var alertTheSelectedValue = function() {
  var val = document.getElementById('txt_name').value;
  var text = $('#emp_names').find('option[value="' + val + '"]').attr('id');
  alert(text);
}
Demo: http://plnkr.co/edit/fJ8aT1PsbftWV8Q6d0tZ?p=preview
 
Share this answer
 
v3
Consider the following code:-

PHP
$hostname = "localhost";
$username = "root";
$passowrd = "";
$database = "test";
$table = "customer";

# Database connection String
$con=mysqli_connect($hostname,$username,$passowrd,$database);
$result = mysqli_query($con, "SELECT ID,Name FROM $table");


$selectbox='<select name=\'register_number\' class=\'input-field\' id=\'registerNumber\' list=\'register_number\' placeholder=\'Select Register No\' >';

while ($row = mysqli_fetch_assoc($result)) {
    $selectbox.='<option id="'. $row['ID'] .'" value="' . $row['ID'] . '">' . $row['Name'] . '</option>';
}

$selectbox.='</select>';

echo $selectbox;



Above code Corresponding HTML format is as following:-

HTML
<select name='register_number' class='input-field' id='registerNumber' list='register_number' placeholder='Select Register No' >
<option id="1" value="1">Rahim</option>
<option id="2" value="2">Karim</option>
<option id="3" value="3">Jamal</option>
<option id="4" value="4">Irfan</option>
</select>


Hope it work well. Happy Coding :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900