Reputation: 31
i am getting the values from database and showing them in the combobox, Now what i want to show selected combobox value in the text field i tried to solve it but i can't kindly help me.....here is my code
<form name="form" method="POST" action="time.php" enctype="multipart/form- data">
<table align="center">
<p>
<tr><td>
<label>Depurture</label></td>
<td>
<script>
function CBtoTB()
{
document.getElementById("text").value=document.getElementById("ComboBox").value
}
</script>
<select>
<option id="ComboBox" onchange="CBtoTB()">Select City</option>
<?php
include ('Database/db.php');
$result=mysql_query("SELECT `ctod` FROM `dewoocandd`") or die(mysql_error());
if($result)
{
while($row=mysql_fetch_array($result))
{
$cname=$row['ctod'];
echo "<option value=\"$cname\" ";
echo'SELECTED';
echo "> ";
echo $row['ctod'];
echo '</option>';
}
}
?>
</select></td></tr></td>
</p>
<p>
<tr><td> <label>Time:</label></td>
<td><input type="text" name="pname" id="text" value="" required></td></tr>
`enter code here` </p>
<tr><td> </td><td align="right"> <input type="submit" name="button" id="button" value="Submit"></td></tr>
</table>
Upvotes: 0
Views: 115
Reputation: 10668
You should attach your onchange
event to the select
element and update the value of the textbox to the value of the select
.
Javascript:
function CBtoTB() {
document.getElementById("text").value = document.getElementById("mySelect").value;
}
HTML:
<select id="mySelect" onchange="CBtoTB()">
<option >Select City</option>
...
</select>
Upvotes: 1
Reputation: 544
id="ComboBox"
and onchange="CBtoTB()"
should be in select tag, not in option tag.
Upvotes: 2