PHP and XML combobox autofilling

Posted on 2010-01-07
Last Modified: 2012-05-08
Hello, i have 2 comboboxes on a page that i am filling through XML, the first one is working fine, but i can't get the second one to be populated correctly, it should be populated depending on the selection of the first combobox! is there any other way than AJAX to get this done ? ..
if not, how could i get that done using AJAX ?
below is the code i'm using.. the javascript does alert the correct value, but i can not get it into php inorder to request the data from my XML upon that value!
also, i do not want to get the page reloaded inorder to get the value throuhg GET or POST!

Thank you.. below is the code i'm using..
<form action="load.php" method="post">Select Cat<select name="categories" id="categories">


	$fff = '<script type="text/javascript">';

	$fff .= 'function getMainCat() {';

	$fff .= "var x = document.getElementById('categories').value;";

	$fff .= 'alert(x); }';

	$fff .= '</script>';


  </select><br /><br />Select Sub Category<select name="sub_categories" id="sub_categories" onchange="getMainCat();">

Open in new window

Question by:SuW
    LVL 17

    Accepted Solution

    Here I coded a sample Javascript for you, create another PHP file which ONLY prints out what you want to get as Ajax output, in my sample I called it ajaxphpfile.php

    That's all
    function ajaxRequest(){
     var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
     if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
      for (var i=0; i<activexmodes.length; i++){
        return new ActiveXObject(activexmodes[i])
        //suppress error
     else if (window.XMLHttpRequest) // if Mozilla, Safari etc
      return new XMLHttpRequest()
      return false
    function getMainCat() {
    var x = document.getElementById('categories').value;
    var mygetrequest=new ajaxRequest();
     if (mygetrequest.readyState==4){
      if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
       document.getElementById("result").innerHTML=mygetrequest.responseText  // HERE YOU PUT WHERE YOU WANT TO GET RESULTS
       alert("An error has occured making the request");
    var categories=encodeURIComponent(document.getElementById("categories").value);"GET", "ajaxphpfile.php?categories="+categories, true)

    Open in new window


    Author Closing Comment

    Thank you.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now