[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


PHP and XML combobox autofilling

Posted on 2010-01-07
Medium Priority
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

CSecurity earned 1000 total points
ID: 26207486
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);
mygetrequest.open("GET", "ajaxphpfile.php?categories="+categories, true)

Open in new window


Author Closing Comment

ID: 31674414
Thank you.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 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