Apr 12, 2007

Dynamically Populate Select List by Ajax

Thanks for visiting.

19 Comments:

Anonymous said...

I ran the scripts and I got 'Array()' printed above the first select list. Why is that?

Thanks,
Sonda

Satya Prakash said...

If you are telling about print_r($_POST) then it is your post-ed data. Initially it will be empty. After submitting the form it will be filled with select element names-values.

Unknown said...

works great, thanks for the script :)

-Andy

Unknown said...

Is it possible to use the values from the selected list that appears with this method later when we submit the form (with $_POST)?

Satya Prakash said...

Why not?
Just check by yourself.
In this page ("dynamic_city_state.php") submit the form after selecting the value. It was tested in Firefox.

Unknown said...

I am doing something wrong. I've tested your code and it works fine. But the code I am using, based on yours, once I change the value of the first select, the second appears in the page correctly, but when I check Page Info it's not there...

Unknown said...

This does not works in IE7 for some weird reason. I tested in firefox and opera it works perfectly but not in IE

Mike_S said...

Hi Satya,

This is cool stuff !, *almost* exactly what I was looking for ;)

What I want to do is to populate a textarea with whatever gets chosen from the select box, i.e. so I can have a list of words available to insert at the current point in the textarea (or anywhere in the textarea, if the cursor thing is a big problem).

I started to modify your code, specifically the city.php file, where I tried a variety of things, but nothing's working yet.

This is probably something very simple for you to do, if you can share the solution, that would be great.

Mike

Satya Prakash said...
This comment has been removed by the author.
Unknown said...

Hi...great form i was wondering though instead of getting the information by hitting the submit button how can you get the information to automatically update in a text field? thanks

Richard Matzinger said...

Thanks. It works great, even in IE7 contrary to the previous comment that claims it doesn't. This is exactly the kind of sample code I needed to get me started.

ElGreco said...

Hi!
I need the same script but my data is in an SQL database.
One Database contains the Country
The other database(users database profile) contains the Country ID (eg.1,2,3) and a name of the city

I would need to to a select from the first database to get a list of the country..and for the latter a select from (users table) where county=X...

i know i did not described it well..but..i hope you did got it

Satya Prakash said...

No problem.
It is same. just use a query for generating country list on Page::dynamic_city_state.php. and instead of passing countryCode pass country id in $_GET['ch']. And on city page you can check which country id it is?
Something like:
if (isset($_GET['ch'])) {

$cID = (integer)$_GET['ch'];

$rst = mysql_query("select id, cname from countryCity where id=$cID");
if row count > 0 {
echo '<select name="cityList">';
while (...) {
// generate data like
/*
<option value="Mumbai">Mumbai</option>
<option value="Delhi">Delhi</option>
*/
}
echo '</select>';
}
}

Unknown said...

I have been trying for a long time to solve a certain problem. What I need is to dynamically populate a select menu with options from a MySQL table. However, the options stored in a cell with comma delimiters. For example, option A, option B. What I need now is to separate the options into separate strings (2+) and place them as:

Note: I could not use brackets, so I used parentheses.

(opening select)
(opening option)option A(closing option)

(opening option)option B(closing option)

(closing select)

Thanks for your help.

Satya Prakash said...

This kind of questions are best suited at PHP forum. Register at any good forum - The Scripts, OZZU, etc

Unknown said...

The code works fine in generally, but I noticed one problem with Firefox (while OK with IE):

when the form elements are separated into different cells of the table for designing the layout, then the form does not seem to recognize the loaded value from the city's select box.

There have never been any problems before with static form elements being separated into different table cells, but apparently this dynamic ajax version does not function well in table cells.

Any suggestions or comments?

Satya Prakash said...

may be you are not using element id correctly!

Jeff Milner said...

Thanks, it worked like a charm!

maan said...

Great Work. I want to extend it to 3 levels means 3 dropdown list.

First list is country
second is state/provinces
3rd is city

every second is populated from the selection of its parent.

Regards
Nauman

 
Privacy Policies
Blogroll