Solved

Dropdown list and populating a textbox

Posted on 2006-11-04
14
744 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ajax and PHP 9 53
Google Dork query 7 66
PHP Web Development 6 24
Special characters in a TCPDF 4 26
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

749 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