How to Create a Drop-Down List Using XML & Ajax in PHP

The Ajax language lets you use a list of XML values to populate a drop-down list in a PHP page. Use this method when you have several values in an XML file that makes it too cumbersome to type in the Ajax code. The Ajax language can be typed within the PHP page where your other Ajax and JavaScript code is located.

Instructions

    • 1

      Right-click the PHP page you want to use to display the drop-down list. Click "Open With" and select your preferred editor.

    • 2

      Open the XML document and instruct the Ajax reader to point to the root node. The root node is the top-level node that explains the content of the XML file. For instance, the following code retrieves a list of books:

      $doc= new DOMDocument();
      $doc->load("books.xml");
      $nodes=$doc->getElementsByTagName('boks');

    • 3

      Loop through each node and display it in the PHP page's drop-down list. The following code writes each element to the drop-down list:

      for ($i=0; $i<=$nodes->length-1; $i++)
      {
      echo "<select>".($nodes->item($i)->parentNode)."</select>";
      }

    • 4

      Save your changes and open the PHP page in a Web browser to review the drop-down list of options.

Related Searches:

References

Comments

Related Ads

Featured