Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Dropdown list and populating a textbox

Posted on 2006-11-04
14
743 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!

 
0
Comment
Question by:paulheinisch
  • 7
  • 4
  • 3
14 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 17874550
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-
0
 
LVL 1

Author Comment

by:paulheinisch
ID: 17875470
I'm getting an undefined in the text box...what am I missing?
0
 
LVL 1

Author Comment

by:paulheinisch
ID: 17875516
Sorry, not an undefined message , but a javascript error stating: "airport is undefinded"
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 49

Expert Comment

by:Roonaan
ID: 17876022
Yes, it should have been airports[selBox.value] rather than airport[selBox.value];

-r-
0
 
LVL 1

Author Comment

by:paulheinisch
ID: 17876605
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!
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17876710
Please try:

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

-r-
0
 
LVL 1

Author Comment

by:paulheinisch
ID: 17877402
Nope...still undefinded
0
 
LVL 1

Expert Comment

by:kurnia
ID: 17878750
change this line:
var airports = new Array();
into this:
var airport = new Array();

:D

HOpe this help,
0
 
LVL 1

Author Comment

by:paulheinisch
ID: 17878862
Yea we caught this one...its was a typo in the first listing...still gets the same results...
0
 
LVL 1

Expert Comment

by:kurnia
ID: 17879318
Do you mind if you paste your HTML result here? Perhaps we can analyze the javascript error.
0
 
LVL 1

Author Comment

by:paulheinisch
ID: 17882130
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>
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17882162
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-
0
 
LVL 1

Expert Comment

by:kurnia
ID: 17882543
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 ;)
0
 
LVL 1

Author Comment

by:paulheinisch
ID: 17882719
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question