Jump to content

[SOLVED] Strange. Works in IE not Firefox


clang

Recommended Posts

I'm working on a Double-Linked Select box. Basically you select an option in box A, and box B becomes filled with that information.

In this case box A is where you select your company, and box B is where you select the branch of that company.

Everything works in IE, but it's not working in Firefox.

Would some one mind taking a look?

 

Here is the xmlhttp file

    //----------------------------------
    // Create xmlhttp request object
    //----------------------------------
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
	xmlhttp = new XMLHttpRequest();
} catch (e) {
	xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
	xmlhttp = window.createRequest();
} catch (e) {
	xmlhttp=false;
}
}

 

Here is the code for select box A and box B. A must be selected before B becomes filled with options.

<tr>
	<th>Company</th>
	<td><select value="Company" onchange="FillBranches(this.value)">
		<option value="Null" SELECTED>Please select a company</option>
		<option value="A">Company A</option>
		<option value="B">Company B</option>
		<option value="C">Company C</option>
	</select></td>
</tr>
<tr>
	<th>Branch</th>
	<td><span id="selectBranch"><select value=\"Branch\">
		<option value=\"-1\">First select a company</option>
		</select></span>
	</td>
</tr>

 

This is the FillBranches functions that gets called when you select a company.

function FillBranches(company) {
  url="./FillBranches.php?c=" + company;
  handleMessages(url, "selectBranch");
}

 

Here is the FillBranches.php page which is called from the FillBranches function

<?php
include 'library/functions.php';

$db=opendb($dbhost, $dbuser, $dbpass, $dbname);

$company = $_GET['c'];
if ($company==''||$company=="Null")
    echo "<select value=\"Branch\">\n<option value=\"-1\">".$company." Please first select a company</option>\n</select>\n";
else {

$result=getCompanyBranchJoin($db,$company);
if($result[0][0])
{
	echo "<SELECT name=\"Branch\">\n";
	echo "<OPTION value=\"00\" SELECTED>All Branches</option>\n";
	for($a=1;$a<count($result);$a++)
	{
			echo "<OPTION value=\"".$result[$a][1]."\">".$result[$a][0]."</OPTION>\n";
	}
	echo '</SELECT>';
}
else
{
	echo 'No branches exist for this company';
}
}
?>

 

And final the handleMessages function which takes the results from FillBranches.php and places them back on our original page.

    //-----------------------------------------------------------
    // Message handler
//  - Sends message to script and waits for completion
    //  - On completion, updates page with script output
//-----------------------------------------------------------
    // url = url to call server-side script
    // resultID = ID of DIV or SPAN element to receive the output
    //-----------------------------------------------------------
function handleMessages (url, resultID) {
  var obj;
  xmlhttp.open("GET", url, true);
  xmlhttp.onreadystatechange=function() {
       if (xmlhttp.readyState==4) {
            obj = document.getElementById(resultID);
            obj.innerHTML = xmlhttp.responseText;
       }
  }
xmlhttp.send();
return 0;
}

 

Again, this works fine in IE, but isn't working at all in Firefox. Any ideas?

Link to comment
Share on other sites

I'm not totally sure whats gone on, but in the end I think all I needed was a xmlhttp.send(null) instead of a xmlhttp.send();

This crap is picky.

Here's the final code for anyone that needs this in the future.

 

XMLHTTP Object

function createRequestObject() {

   var req;

   if(window.XMLHttpRequest){
      // Firefox, Safari, Opera...
      req = new XMLHttpRequest();
   } else if(window.ActiveXObject) {
      // Internet Explorer 5+
      req = new ActiveXObject("Microsoft.XMLHTTP");
   } else {
      // There is an error creating the object,
      // just as an old browser is being used.
      alert('Problem creating the XMLHttpRequest object');
   }

   return req;

}

// Make the XMLHttpRequest object
var xmlhttp = createRequestObject();

 

Select Boxes Code

<tr>
	<th>Company</th>
	<td><select value="Company" onchange="FillBranches(this.value)">
		<option value="Null" SELECTED>Please select a company</option>
		<option value="A">Company A</option>
		<option value="B">Company B</option>
		<option value="C">Company C</option>
	</select></td>
</tr>
<tr>
	<th>Branch</th>
	<td><span id="selectBranch"><select value=\"Branch\">
		<option value=\"-1\">First select a company</option>
		</select></span>
	</td>
</tr>

 

FillBranches

function FillBranches(company) {
  url="./FillBranches.php?c=" + company;
  handleMessages(url, "selectBranch");
}

 

FillBranches.php

<?php
include 'library/functions.php';

$db=opendb($dbhost, $dbuser, $dbpass, $dbname);

$company = $_GET['c'];
if ($company==''||$company=="Null")
    echo "<select value=\"Branch\">\n<option value=\"-1\">".$company." Please first select a company</option>\n</select>\n";
else {

$result=getCompanyBranchJoin($db,$company);
if($result[0][0])
{
	echo "<SELECT name=\"Branch\">\n";
	echo "<OPTION value=\"00\" SELECTED>All Branches</option>\n";
	for($a=1;$a<count($result);$a++)
	{
			echo "<OPTION value=\"".$result[$a][1]."\">".$result[$a][0]."</OPTION>\n";
	}
	echo '</SELECT>';
}
else
{
	echo 'No branches exist for this company';
}
}
?>

 

And finally HandleMessages

function handleMessages (url, resultID) {
  xmlhttp.open("GET", url,true);
  xmlhttp.onreadystatechange=function(){
   if(xmlhttp.readyState == 4&& xmlhttp.status==200){
	var response = xmlhttp.responseText;
	document.getElementById(resultID).innerHTML = response;
   }
}
  xmlhttp.send(null);
  return 0;
}

 

Hope this help some one at some point. As far as I can tell it's working fine in FF and IE.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.