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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dkyadav80
    New Member
    • Jul 2008
    • 21

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

    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.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    What have you managed so far? This is the classical dependent drop-down problem. Post your code.

    Comment

    • dkyadav80
      New Member
      • Jul 2008
      • 21

      #3
      xml CDATA SECTION

      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
      [Code=xml]
      <cities>
      <city><![CDATA[Abohar]]></city>
      <city><![CDATA[Abu Road]]></city>
      <city><![CDATA[Adilabad]]></city>
      <city><![CDATA[Ariyalur]]></city>
      <city><![CDATA[Agartala]]></city>
      <city><![CDATA[Agra]]></city>
      <city><![CDATA[Ahmedabad]]></city>
      <city><![CDATA[Ahmednagar]]></city>
      <city><![CDATA[Aizwal]]></city>
      <city><![CDATA[Ajmer]]></city>
      <city><![CDATA[Ajjampura]]></city>
      <city><![CDATA[Akola]]></city>
      <city><![CDATA[Aligarh]]></city>
      </cities>[/code]

      [php]

      function getCities() {
      $filename = "cities.xml ";
      if (file_exists($f ilename)) {
      $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
      [code=xml]

      <states>
      <state><![CDATA[Andaman and Nicobar Islands]]></state>
      <state><![CDATA[Andhra Pradesh]]></state>
      <state><![CDATA[Arunachal Pradesh]]></state>
      <state><![CDATA[Assam]]></state>
      <state><![CDATA[Bihar]]></state>
      <state><![CDATA[Chandigarh]]></state>
      <state><![CDATA[Chhattisgarh]]></state>
      <state><![CDATA[Dadra and Nagar Haveli]]></state>
      <state><![CDATA[Daman and Diu]]></state>
      <state><![CDATA[Delhi]]></state>
      <state><![CDATA[Goa]]></state>
      <state><![CDATA[Gujarat]]></state>
      <state><![CDATA[Haryana]]></state>
      <state><![CDATA[Himachal Pradesh]]></state>
      <state><![CDATA[Jammu and Kashmir]]></state>
      <state><![CDATA[Jharkhand]]></state>
      <state><![CDATA[Karnataka]]></state>
      <state><![CDATA[Kerala]]></state>
      </states>[/code]

      Php:
      [Code=php]

      function getStates(){
      $filename="stat es.xml";
      if (file_exists($f ilename)) {
      $states_obj = simplexml_load_ file($filename) ;
      $states = $states_obj->xpath('/states/state');
      return $states;
      } else {
      echo "File does not exist";
      }
      }
      $states = getStates();
      $main_smarty->assign("states ", $states);[/code]


      [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_p rof_city" style="width:18 0px" onChange="displ ay(this,'ot');" >
      <option value="Select" selected="selec ted">Select</option>
      {html_options values=$cities output=$cities}
      <option value="other">o ther</option>
      </select></td>
      <td align="left">
      <input name="ocity" id="ot" type="text" style="display: none;width:170p x" maxlength="50"/>
      </td>
      </tr>
      <tr>
      <td align="right">< label for="cons_indi_ prof_state">Sta te<span class="req">*</span></label></td>
      <td align="left">
      <select name="cons_indi _prof_state" id="cons_indi_p rof_state" style="width:18 0px" onChange="displ ay2(this,'oth') ;">
      <option value="Select" selected="selec ted">Select</option>
      {html_options values=$states output=$states}
      <option value="other">o ther</option>
      </select></td>
      <td align="left">
      <input name="ostate" id="oth" type="text" style="display: none;width:170p x" 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
      Last edited by acoder; Aug 8 '08, 11:58 AM. Reason: Added [code] tags

      Comment

      • dkyadav80
        New Member
        • Jul 2008
        • 21

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

        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.

        Comment

        • newlearner
          New Member
          • May 2007
          • 67

          #5
          Can u give the structure of the XML that has the city and state combination.

          Are u parsing the XML using javascript or PHP?

          Comment

          • dkyadav80
            New Member
            • Jul 2008
            • 21

            #6
            Originally posted by newlearner
            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
            [Code=xml]: goes here
            <cities>
            <city><![CDATA[Abohar]]></city>
            <city><![CDATA[Abu Road]]></city>
            <city><![CDATA[Adilabad]]></city>
            <city><![CDATA[Ariyalur]]></city>
            <city><![CDATA[Agartala]]></city>
            <city><![CDATA[Agra]]></city>
            <city><![CDATA[Ahmedabad]]></city>
            <city><![CDATA[Ahmednagar]]></city>
            <city><![CDATA[Aizwal]]></city>
            <city><![CDATA[Ajmer]]></city>
            <city><![CDATA[Ajjampura]]></city>
            <city><![CDATA[Akola]]></city>
            <city><![CDATA[Aligarh]]></city>
            </cities>[/code]

            [: code goes here for cities]
            [code=php]
            function getCities() {
            $filename = "cities.xml ";
            if (file_exists($f ilename)) {
            $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);[/code]


            file: states.xml
            [code=xml]: goes here

            <states>
            <state><![CDATA[Andaman and Nicobar Islands]]></state>
            <state><![CDATA[Andhra Pradesh]]></state>
            <state><![CDATA[Arunachal Pradesh]]></state>
            <state><![CDATA[Assam]]></state>
            <state><![CDATA[Bihar]]></state>
            <state><![CDATA[Chandigarh]]></state>
            <state><![CDATA[Chhattisgarh]]></state>
            <state><![CDATA[Dadra and Nagar Haveli]]></state>
            <state><![CDATA[Daman and Diu]]></state>
            <state><![CDATA[Delhi]]></state>
            <state><![CDATA[Goa]]></state>
            <state><![CDATA[Gujarat]]></state>
            <state><![CDATA[Haryana]]></state>
            <state><![CDATA[Himachal Pradesh]]></state>
            <state><![CDATA[Jammu and Kashmir]]></state>
            <state><![CDATA[Jharkhand]]></state>
            <state><![CDATA[Karnataka]]></state>
            <state><![CDATA[Kerala]]></state>
            </states>[/code]

            Php:
            [Code: ghes here]
            [code=php]
            function getStates(){
            $filename="stat es.xml";
            if (file_exists($f ilename)) {
            $states_obj = simplexml_load_ file($filename) ;
            $states = $states_obj->xpath('/states/state');
            return $states;
            } else {
            echo "File does not exist";
            }
            }
            $states = getStates();
            $main_smarty->assign("states ", $states);[/code]


            [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_p rof_city" style="width:18 0px" onChange="displ ay(this,'ot');" >
            <option value="Select" selected="selec ted">Select</option>
            {html_options values=$cities output=$cities}
            <option value="other">o ther</option>
            </select></td>
            <td align="left">
            <input name="ocity" id="ot" type="text" style="display: none;width:170p x" maxlength="50"/>
            </td>
            </tr>
            <tr>
            <td align="right">< label for="cons_indi_ prof_state">Sta te<span class="req">*</span></label></td>
            <td align="left">
            <select name="cons_indi _prof_state" id="cons_indi_p rof_state" style="width:18 0px" onChange="displ ay2(this,'oth') ;">
            <option value="Select" selected="selec ted">Select</option>
            {html_options values=$states output=$states}
            <option value="other">o ther</option>
            </select></td>
            <td align="left">
            <input name="ostate" id="oth" type="text" style="display: none;width:170p x" 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
            Last edited by acoder; Aug 8 '08, 11:43 AM. Reason: fixed code tags

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              dkyadav80, learn to use code tags properly. I'll show you how:
              &#91;CODE=javas cript]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.

              Comment

              Working...