We help IT Professionals succeed at work.

Dropdown list and populating a textbox

paulheinisch
paulheinisch asked
on
Medium Priority
771 Views
Last Modified: 2013-12-12
I have the dropdown list working for me, but I want to be able to fill a textbox with an item from the same mysql table. The dropdown list display one item from the table and when the user selects the item from the drop down list, I need it to display another item from that record. So for example the sql database has a table called airports. Within the table three fields: id, name, city. The drop down list shows the name, i want the textbox to then show the city. Help please!

 
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2006
Commented:
When your query is like this:

<?php
  $query = 'SELECT id, name, city FROM airports';
?>

You can have something like this:

<?php
   $selectBoxOptions = '';
   $javascript = '';
   
   $res = mysql_query($query);
   while($row = mysql_fetch_assoc($res)) {
       $id = $row['id'];
       $city = addslashes($row['city']);
       $javascript .= "\nairports[$id] = '$city';";
       $selectBoxOptions .= '<option value="'.$id.'">'.htmlspecialchars($row['name']).'</option>';
   }
?>

Then output the $javascript inside its own javascript block and $selectBox inside a <select> tag:

<script type="text/javascript">
var airports = new Array();
<?php echo $javascript;?>
function setCity(selBox) {
  selBox.form.city.value = airport[selBox.value];
}
</script>
<form>
  <select name="airport" onchange="setCity(this);">
  <?php echo $selectBoxOptions;?>
  </select>
  <input type="text" name="city" disabled="disabled" />
</form>

-r-

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
I'm getting an undefined in the text box...what am I missing?

Author

Commented:
Sorry, not an undefined message , but a javascript error stating: "airport is undefinded"
CERTIFIED EXPERT
Top Expert 2006

Commented:
Yes, it should have been airports[selBox.value] rather than airport[selBox.value];

-r-

Author

Commented:
Yea I did catch that...what happens now (after changing the airport to airports) is when I select an item on the dropdownlist within the textbox I get undefined...so I know we are close!
CERTIFIED EXPERT
Top Expert 2006

Commented:
Please try:

function setCity(selBox) {
  selBox.form.city.value = airport[selBox.options[selBox.selectedIndex].value];
}

-r-

Author

Commented:
Nope...still undefinded

Commented:
change this line:
var airports = new Array();
into this:
var airport = new Array();

:D

HOpe this help,

Author

Commented:
Yea we caught this one...its was a typo in the first listing...still gets the same results...

Commented:
Do you mind if you paste your HTML result here? Perhaps we can analyze the javascript error.

Author

Commented:
Here's the source output...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
      
<script type="text/javascript">
var trips = new Array();

1 = 'Venice';
2 = 'Los Angeles';function setDestination(selBox) {
  selBox.form.city.value = trips[selBox.options[selBox.selectedIndex].value];
;
}
</script>
<form name="test">
  <select name="trip" onchange="setDestination(this);">
  <option value="1">Greek Isles Oddessey</option><option value="2">Alaska Adventure</option>  </select>
  <input type="text" name="city" disabled="disabled" />
</form>

</body>
</html>
CERTIFIED EXPERT
Top Expert 2006

Commented:
hmmm. that should have been:

<script type="text/javascript">
var trips = new Array();

trips[1] = 'Venice';
trips[2] = 'Los Angeles';function setDestination(selBox) {
  selBox.form.city.value = trips[selBox.options[selBox.selectedIndex].value];
;
}


Can you double check that the line below is as it is:
$javascript .= "\ntrips[".$id."] = '$city';";

-r-

Commented:
Roonaan is right, you need to correctly asign the array whith trips[1] = 'Venice', instead of 1='Venice'.

On more thing, please check in the asignment to the textbox, I think it should be:
document.test.city.value = trips[selBox.options[selBox.selectedIndex].value];

Hope this help ;)

Author

Commented:
I found the mistake I was making- thank you ...yes I should have had this:

$javascript .= "\ntrips[$id] = '$trip_dest';";


var trips = new Array();


opps! Thank you
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.