-1

I have a drop down list:

<select  class="form-control" name = "Terminal_ID" id = "Terminal_ID" onchange="updateValue();">
<option>Select</option>
<?php
    $query="SELECT *  FROM mytable";
    $result=mysqli_query($con,$query);
    while($rows=mysqli_fetch_array($result2))
    {
            $tid=$rows['Idcolumn'];
            $tn=$rows['Namecolumn'];
    ?>  
    <option value="<?=$tn;?>"><?=$tid;?></option> 
    <?php
    }
    ?>
</select>

and a text input box

<input class="form-control" type="text" id="textboxid" name="tname" />

I have this on updateValue function in javascript:

function updateValue()
    {
$('#textboxid').val($('#Terminal_ID').val());
    }

    $(document).ready(function () {
    updateValue();
});

It work's, since it shows the value of $tn in my text input box. The problem is that, the function change the value of the selection in the dropdown list. and if, i want to use the selection of the drop down to insert some data in my database it sends me the $tn value, and i need the $tid value. If I change this <option value="<?=$tn;?>"> to <option value="<?=$tid;?>"> it change the value of the text input box. Is there anything i can modify to this code to get the $tn as value in text input box and the $tid as value of the selection?

Ahmad Hassan
  • 371
  • 4
  • 22
irena
  • 81
  • 9
  • None of this questions answer to the mine, since i have to take values from my database and i can't use the text or the value of the drop down list. I have already done that. What i need is to fill the text input box having as condition the selection on the dropdown list. Pleas don't mark it as dublicate – irena Sep 27 '17 at 15:25

2 Answers2

0

if you set your html option like this,

HTML

<option value="<?=$tid;?>"><?=$tn;?></option> 

JS

function updateValue()
{
  $('#textboxid').val($('#Terminal_ID option:selected').text()); // this will set name 
}

$(document).ready(function () {
    updateValue();
});
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

you can update you function like below that fix your problem

    function updateValue()
    {
    $('#textboxid').val($('#Terminal_ID option:selected').text());
    }
sonsor
  • 31
  • 2