Mazhar Iqbal Rana
Mazhar Iqbal Rana

Reputation: 57

Dropdown Selected value in Textbox PHP

***Possible solution given in other question is not working here...

Hi I want to select value from dropdown and fetch in Textbox

This below code is fetching value from database but it is not putting in textbox. textbox below is named as emp_number and name of my dropdown is dealer

Please have a look

<?php


include 'connect-db.php';

//mysql_select_db("my", $con);
$s=mysql_query("select * from employees order by emp_number asc "); 
?>
   Select Employee Number:

   <select name="dealer" id="dealer" onchange='updateMyText()'>
   <option value="">---- Select employee Number -----</option>

    <?php
  while($dd=mysql_fetch_array($s))
{
?>
    <option value="<?php echo $dd['emp_id'] ?>"><?php echo $dd['emp_number']       ?></option>
 <?php
}
    ?>                  
</select>

<html>
<head>
        <title>PHP insertion</title>
    <link rel="stylesheet" href="css/insert.css" />
    <link rel="stylesheet" href="css/navcss.css" />
    <script type='text/javascript'>
    /*$('#dealer').change(function () {
         $("#emp_number").val($(this).val());*/
             function updateMyText()
{
var dd = document.getElementById("dealer");
var ddtext = dd.options[dd.selectedIndex].text;
document.getElementById('emp_number').value = ddtext;
}
</script>

    </head>
    <body>
            <div class="maindiv">
                    <br />
                    <label>Employee Number:</label>
                <br />
                <input class="input" type="text" name="emp_number" value="" />
                </div>
 </body>
</html> 

Upvotes: 0

Views: 5658

Answers (2)

aimprogman
aimprogman

Reputation: 294

"document.getElementById('emp_number').value"--mistake.

Your javascript should be like

<script type='text/javascript'>
/*$('#dealer').change(function () {
     $("#emp_number").val($(this).val());*/
function updateMyText()
{
var dd = document.getElementById("dealer");
var ddtext = dd.options[dd.selectedIndex].text;
document.getElementsByName('emp_number')[0].value=ddtext;
}
</script>

Upvotes: 2

Asuquo12
Asuquo12

Reputation: 835

Your statements does not ending syntax ";"

<option value="<?php echo $dd['emp_id']; ?>"><?php echo $dd['emp_number'] ;?></option>

It should have thrown syntax error actually.

while($dd = mysqli_fetch_array($s))
{
 <option><?php echo $dd['emp_id'];?>|<?php echo $dd['emp_number']; ?></option>

}

Upvotes: 0

Related Questions