Results 1 to 3 of 3

Thread: pulling info from php into a html menu

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    2

    pulling info from php into a html menu

    Hey all, cant figure something out. i feel its something so simple!

    The task is:

    using javascript, take the names of games in this php (http://newmedia.purchase.edu/~sburdi...game_names.php) and put them in a pull down menu.

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: pulling info from php into a html menu

    You need to use AJAX to grab the XML file parse it and display it on your page

    The xml file that you want to read from must be on the same domain as the file thats reading it, so if this file is called page.htm and is on mydomain.com the xml file must be on mydomain.com as well.

    This reads the xml file and places the Game_Name from the XML into a listbox.
    Code:
    <select id="menu"></select>
    
    <script language="javascript">
    
    function createRequestObject()
    {
    	var request_o; //declare the variable to hold the object.
    	var browser = navigator.appName; //find the browser name
    	if(browser == "Microsoft Internet Explorer"){
    		/* Create the object using MSIE's method */
    		request_o = new ActiveXObject("Microsoft.XMLHTTP");
    	}else{
    		/* Create the object using other browser's method */
    		request_o = new XMLHttpRequest();
    	}
    	return request_o; //return the object
    }
    
    function handleMenu()
    {
    	try
    	{
    		if(http.readyState == 4){ //Finished loading the response
    			var nodes = http.responseXML;
    			alert(nodes.getElementsByTagName('Game').length);
    			
    			
    			
    			for(i = 0; i < nodes.getElementsByTagName('Game').length; i++)
    			{
    				menuItem = document.createElement("option");
    				menuItem.appendChild(document.createTextNode(nodes.getElementsByTagName('Game_Name')[i].firstChild.data));
    				document.getElementById('menu').appendChild(menuItem);
    			}
    		
    		}
    	}
    	catch(Exception)
    	{
    	}
    
    }
    
    var http = createRequestObject();
    function getMenu()
    {
    	try
    	{ 
    	http.open('get', 'game_names.xml');
    	http.onreadystatechange = handleMenu; 
    	http.send(null);
    	}
    	catch(Exception)
    	{
    		http = createRequestObject();
    		getMenu();
    	}
    }
    getMenu();
    </script>

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    2

    Re: pulling info from php into a html menu

    thanks a lot. works fine

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width