Solved

Dropdown list and populating a textbox

Posted on 2006-11-04
14
739 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
Comment Utility
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
Comment Utility
I'm getting an undefined in the text box...what am I missing?
0
 
LVL 1

Author Comment

by:paulheinisch
Comment Utility
Sorry, not an undefined message , but a javascript error stating: "airport is undefinded"
0
 
LVL 49

Expert Comment

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

-r-
0
 
LVL 1

Author Comment

by:paulheinisch
Comment Utility
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
Comment Utility
Please try:

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

-r-
0
 
LVL 1

Author Comment

by:paulheinisch
Comment Utility
Nope...still undefinded
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Expert Comment

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

:D

HOpe this help,
0
 
LVL 1

Author Comment

by:paulheinisch
Comment Utility
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
Comment Utility
Do you mind if you paste your HTML result here? Perhaps we can analyze the javascript error.
0
 
LVL 1

Author Comment

by:paulheinisch
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now