473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Populating City,state from Drop Down Menu

hello. i have 3 tables "city" containing id, city, state_id and "states" containing id, states and "table" containing a city field (INT 11)

what i want to do is to select a state then select a city based on the "state_id" and the result to be stored into the "city" field inside the "table" table.

the code will look like this:
Expand|Select|Wrap|Line Numbers
  1. $city = isset($_REQUEST['city']) ? addslashes($_REQUEST['city']) : 0;
  2.     $state = isset($_REQUEST['state']) ? addslashes($_REQUEST['state']) : '';
  3.  
  4. <table>
  5.     <tr>
  6.     <td>Search by Departure County
  7.               <select name="state" id="state" onChange="return getCity(this.value);">
  8.                 <option value="">Select State</option>
  9.                 {section name=res loop=$resultS}
  10.                   <option value="{$resultS[res].id}" {if $state == $resultS[res].id} selected {/if}>{$resultS[res].state}</option>
  11.                 {/section}
  12.               </select>
  13.              </td>
  14.                        <td id="city_td">
  15.               {if isset($cityC)}   
  16.               <select name="city" id="city" onchange='return getCityResult(this.value);'>
  17.                 <option value="">Select County</option>
  18.                 {section name=res loop=$cityC}
  19.                   <option value="{$cityC[res].id}" {if $city == $cityC[res].id} selected {/if}>{$cityC[res].city}</option>
  20.                 {/section}
  21.               </select>
  22.               {else}
  23.               <select name="city" id="city">
  24.                 <option value="">Select County</option>
  25.               </select>
  26.               {/if}
  27.              </td>
  28.                      </tr>
  29.                     </table>
  30.  
i can populate the state menu like this:
Expand|Select|Wrap|Line Numbers
  1. /**State**/
  2.   $sqlS = 'SELECT * FROM states ORDER BY state ASC';
  3.     $qryResult = $db->query($sqlS);
  4.  
  5.     if(DB::isError($qryResult))
  6.     {
  7.         die($qryResult->getMessage());
  8.     }
  9.     $i = 0;
  10.     $resultS = array();
  11.     while($row = $qryResult->fetchRow(DB_FETCHMODE_ASSOC))
  12.     {
  13.         $tmp = array(
  14.                     'id' => $row['id'],
  15.                     'state' => $row['state']
  16.           );
  17.         $resultS[$i++] = $tmp;
  18.     }
  19.     //$smarty->assign('archive', 1);
  20.   $smarty->assign('resultS', $resultS);
  21.   //print_r($resultS);
  22. /**State**/
  23.  
but i don't know how to get the values into the CITY drop down menu and then insert data into the CITY field inside the "table" table.

thanks a lot
Sep 16 '10 #1
1 3107
dlite922
1,584 Expert 1GB
Welcome back patrioticcow!

You can do this 2 ways:

1. Upon the state select box's onchange() event submit the form and redisplay the page now with the city populated and so on so forth for however many dependent dropdowns you have.

2. Use AJAX to do the same without submitting the form and thus refreshing the page. A bit nicer but heavier in the JavaScript coding. Use a JS library like jQuery to make your life easier.



Dan
Sep 22 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Mike Wilcox | last post by:
I use the following code snippet to build a drop down menu with the results of a query. How can I set the initial value of this input based on the result of another query? What I am trying to do...
1
by: B. G. Mahesh | last post by:
hi In the registration form I have city, state, country fields. I was wondering if there was a database available on the net which has the list of states in each of the countries. That way when...
4
by: Angie H. | last post by:
Anyone know how to create a drop down menu where the user can enter text also? If they do not want to choose an answer from the drop down menu, the users can just enter their own answers in the...
1
by: Vic Spainhower | last post by:
I have 2 drop-down menus (1) States (2) Shows By State. My question is how do I invoke a re-build of the Shows By State drop-down when the user requests a different state. I believe this has to be...
1
by: Vic Spainhower | last post by:
I have 2 drop-down menus (1) States (2) Shows By State. My question is how do I invoke a re-build of the Shows By State drop-down when the user requests a different state. I believe this has to be...
3
by: Novice Computer User | last post by:
I have a drop down menu where people are able to select 1 item. However, I want to modify the drop down list so that the person can select more than 1 item (i.e by holding down the shift button...
12
keyvanrahmadi
by: keyvanrahmadi | last post by:
Hello every one, I have just recently started learning Mysql/PHP and i am trying to design a page with drop down menus which will allow the user to select what they want to see and submit the...
2
by: millertime90 | last post by:
Hi basically my problem is I have 2 drop down menus populated by my database the first populated by a field in the database and the 2nd populated with a relation to the value selected in the first...
1
by: smaheshwari84 | last post by:
hello i need a help regarding populating a drop down menu from the values in an database when a particular option is chosen in the previous menu. let there be two menus one list the type of...
6
by: dkyadav80 | last post by:
Hi sir, I'm new about xml, javascript. I have two selection field(html) first is city and second is state. the city and state values should be store in xml file. when user select city then all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.