By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,598 Members | 1,897 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,598 IT Pros & Developers. It's quick & easy.

display the value from xml file when user click the drop down menu

P: 21
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 city values should display in city selection field and when user select any one city value then this state of this city should be display auto in state
selection field without user selection. please solve my prolem.

Thank you.
Jul 19 '08 #1
Share this Question
Share on Google+
6 Replies


acoder
Expert Mod 15k+
P: 16,027
What have you managed so far? This is the classical dependent drop-down problem. Post your code.
Jul 19 '08 #2

P: 21
Hi Sir,
I'm new in XML , I 'm displaying the cities values in drop-down menu box from cities.xml file for city: field, through smarty and php code , on html form and I have next field is State which is also drop-down menu list box. I want when i select city name from city drop-down then state name of this city will become automatic in state drop-down menu box. currently i'm using saparate xml file for city and state. both xml file are below:

file: cities.xml
Expand|Select|Wrap|Line Numbers
  1. <cities>
  2.     <city><![CDATA[Abohar]]></city>
  3.     <city><![CDATA[Abu Road]]></city>
  4.     <city><![CDATA[Adilabad]]></city>
  5.     <city><![CDATA[Ariyalur]]></city>
  6.     <city><![CDATA[Agartala]]></city>
  7.     <city><![CDATA[Agra]]></city>
  8.     <city><![CDATA[Ahmedabad]]></city>
  9.     <city><![CDATA[Ahmednagar]]></city>
  10.     <city><![CDATA[Aizwal]]></city>
  11.     <city><![CDATA[Ajmer]]></city>
  12.     <city><![CDATA[Ajjampura]]></city>
  13.     <city><![CDATA[Akola]]></city>
  14.     <city><![CDATA[Aligarh]]></city>
  15. </cities>
[php]

function getCities() {
$filename = "cities.xml";
if (file_exists($filename)) {
$cities_obj = simplexml_load_file($filename);
$cities = $cities_obj->xpath('/cities/city');
return $cities;
} else {
echo "File does not exist";
}
}
$cities = getCities();
$main_smarty->assign("cities", $cities);[/php]


file: states.xml
Expand|Select|Wrap|Line Numbers
  1.  
  2. <states>
  3.     <state><![CDATA[Andaman and Nicobar Islands]]></state>
  4.     <state><![CDATA[Andhra Pradesh]]></state>
  5.     <state><![CDATA[Arunachal Pradesh]]></state>
  6.     <state><![CDATA[Assam]]></state>
  7.     <state><![CDATA[Bihar]]></state>
  8.     <state><![CDATA[Chandigarh]]></state>
  9.     <state><![CDATA[Chhattisgarh]]></state>
  10.     <state><![CDATA[Dadra and Nagar Haveli]]></state>
  11.     <state><![CDATA[Daman and Diu]]></state>
  12.     <state><![CDATA[Delhi]]></state>
  13.     <state><![CDATA[Goa]]></state>
  14.     <state><![CDATA[Gujarat]]></state>
  15.     <state><![CDATA[Haryana]]></state>
  16.     <state><![CDATA[Himachal Pradesh]]></state>
  17.     <state><![CDATA[Jammu and Kashmir]]></state>
  18.     <state><![CDATA[Jharkhand]]></state>
  19.     <state><![CDATA[Karnataka]]></state>
  20.     <state><![CDATA[Kerala]]></state>
  21. </states>
Php:
Expand|Select|Wrap|Line Numbers
  1.  
  2. function getStates(){
  3.     $filename="states.xml";
  4.     if (file_exists($filename)) {
  5.         $states_obj = simplexml_load_file($filename);
  6.         $states = $states_obj->xpath('/states/state');
  7.         return $states;
  8.     } else {
  9.         echo "File does not exist";
  10.     }
  11. }
  12. $states = getStates();    
  13. $main_smarty->assign("states", $states);

[HTML]

<tr>
<td align="right"><label for="cons_indi_prof_city">City<span class="req">*</span></label></td>
<td align="left">
<select name="cons_indi_prof_city" id="cons_indi_prof_city" style="width:180px" onChange="display(this,'ot');">
<option value="Select" selected="selected">Select</option>
{html_options values=$cities output=$cities}
<option value="other">other</option>
</select></td>
<td align="left">
<input name="ocity" id="ot" type="text" style="display:none;width:170px" maxlength="50"/>
</td>
</tr>
<tr>
<td align="right"><label for="cons_indi_prof_state">State<span class="req">*</span></label></td>
<td align="left">
<select name="cons_indi_prof_state" id="cons_indi_prof_state" style="width:180px" onChange="display2(this,'oth');">
<option value="Select" selected="selected">Select</option>
{html_options values=$states output=$states}
<option value="other">other</option>
</select></td>
<td align="left">
<input name="ostate" id="oth" type="text" style="display:none;width:170px" maxlength="50" />
</td>
</tr>[/html]

//////////

All codes are working good , cities and states are displaying good in drow-down menu box. Proble is that if user select the city then particular state of that's city will should be display in state drop down menu box.
Pls solve my problem I'm sufffering from 2 weeks.

Thanks
Aug 1 '08 #3

P: 21
Hi guys,

I 'm developing a site using javascript lib file with prototype, Iframe, smarty , php html, css.. I have read the xml file( city and state ) using CDATASECTION through php and display by smarty in drop down menu . I have one text filed after drop down. i wanna when user select the city from drop down then auto display the state of this city which one user is selecting in the text field.
pls give programe example.

Pls help me.
Aug 8 '08 #4

P: 67
Can u give the structure of the XML that has the city and state combination.

Are u parsing the XML using javascript or PHP?
Aug 8 '08 #5

P: 21
Can u give the structure of the XML that has the city and state combination.

Are u parsing the XML using javascript or PHP?


Yes sir, I'm giving below in detail:
-> I'm parsing through PHP

I 'm displaying the cities values in drop-down menu box from cities.xml file for city: field, through smarty and php code , on html form and I have next field is State which is also drop-down menu list box. I want when i select city name from city drop-down then state name of this city will become automatic in state drop-down menu box. currently i'm using saparate xml file for city and state. both xml file are below:

file: cities.xml
Expand|Select|Wrap|Line Numbers
  1. : goes here
  2. <cities>
  3. <city><![CDATA[Abohar]]></city>
  4. <city><![CDATA[Abu Road]]></city>
  5. <city><![CDATA[Adilabad]]></city>
  6. <city><![CDATA[Ariyalur]]></city>
  7. <city><![CDATA[Agartala]]></city>
  8. <city><![CDATA[Agra]]></city>
  9. <city><![CDATA[Ahmedabad]]></city>
  10. <city><![CDATA[Ahmednagar]]></city>
  11. <city><![CDATA[Aizwal]]></city>
  12. <city><![CDATA[Ajmer]]></city>
  13. <city><![CDATA[Ajjampura]]></city>
  14. <city><![CDATA[Akola]]></city>
  15. <city><![CDATA[Aligarh]]></city>
  16. </cities>
[: code goes here for cities]
Expand|Select|Wrap|Line Numbers
  1. function getCities() {
  2. $filename = "cities.xml";
  3. if (file_exists($filename)) {
  4. $cities_obj = simplexml_load_file($filename);
  5. $cities = $cities_obj->xpath('/cities/city');
  6. return $cities;
  7. } else {
  8. echo "File does not exist";
  9. }
  10. }
  11. $cities = getCities();
  12. $main_smarty->assign("cities", $cities);

file: states.xml
Expand|Select|Wrap|Line Numbers
  1. : goes here
  2.  
  3. <states>
  4. <state><![CDATA[Andaman and Nicobar Islands]]></state>
  5. <state><![CDATA[Andhra Pradesh]]></state>
  6. <state><![CDATA[Arunachal Pradesh]]></state>
  7. <state><![CDATA[Assam]]></state>
  8. <state><![CDATA[Bihar]]></state>
  9. <state><![CDATA[Chandigarh]]></state>
  10. <state><![CDATA[Chhattisgarh]]></state>
  11. <state><![CDATA[Dadra and Nagar Haveli]]></state>
  12. <state><![CDATA[Daman and Diu]]></state>
  13. <state><![CDATA[Delhi]]></state>
  14. <state><![CDATA[Goa]]></state>
  15. <state><![CDATA[Gujarat]]></state>
  16. <state><![CDATA[Haryana]]></state>
  17. <state><![CDATA[Himachal Pradesh]]></state>
  18. <state><![CDATA[Jammu and Kashmir]]></state>
  19. <state><![CDATA[Jharkhand]]></state>
  20. <state><![CDATA[Karnataka]]></state>
  21. <state><![CDATA[Kerala]]></state>
  22. </states>
Php:
[Code: ghes here]
Expand|Select|Wrap|Line Numbers
  1. function getStates(){
  2. $filename="states.xml";
  3. if (file_exists($filename)) {
  4. $states_obj = simplexml_load_file($filename);
  5. $states = $states_obj->xpath('/states/state');
  6. return $states;
  7. } else {
  8. echo "File does not exist";
  9. }
  10. }
  11. $states = getStates(); 
  12. $main_smarty->assign("states", $states);

[and HTML CODE GOES HERE] : for city and state ;
[html]
<tr>
<td align="right"><label for="cons_indi_prof_city">City<span class="req">*</span></label></td>
<td align="left">
<select name="cons_indi_prof_city" id="cons_indi_prof_city" style="width:180px" onChange="display(this,'ot');">
<option value="Select" selected="selected">Select</option>
{html_options values=$cities output=$cities}
<option value="other">other</option>
</select></td>
<td align="left">
<input name="ocity" id="ot" type="text" style="display:none;width:170px" maxlength="50"/>
</td>
</tr>
<tr>
<td align="right"><label for="cons_indi_prof_state">State<span class="req">*</span></label></td>
<td align="left">
<select name="cons_indi_prof_state" id="cons_indi_prof_state" style="width:180px" onChange="display2(this,'oth');">
<option value="Select" selected="selected">Select</option>
{html_options values=$states output=$states}
<option value="other">other</option>
</select></td>
<td align="left">
<input name="ostate" id="oth" type="text" style="display:none;width:170px" maxlength="50" />
</td>
</tr>[/html]

//////////

All codes are working good , cities and states are displaying good in drow-down menu box. Proble is that if user select the city then particular state of that's city will should be display in state drop down menu box.
Pls solve my problem I'm sufffering from 2 weeks.

Thanks
Aug 8 '08 #6

acoder
Expert Mod 15k+
P: 16,027
dkyadav80, learn to use code tags properly. I'll show you how:
[CODE=javascript]var test = "test";[/code]

Replace "javascript" with something else for other languages. You can even omit the language, but at the very least use the code tags properly.

I've merged this thread with one you asked earlier since it relates to the same problem.
Aug 8 '08 #7

Post your reply

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