Sign in to follow this  
chbrules

Javascript

Recommended Posts

I have 2 requests. One, I'd like to know how I can dynamically call returned values from a page to dynamically add to a list which is cross-browser compatible. Currently a friend of mine helped make a little javascript snippet that uses ActiveX to retrieve the results of a PHP/MySQL query on another page and organize into a list dynamically on a page. I've only tried it in IE and firefox, of course firefox doesn't like it. =/ Here's the code:
<Script Language="javascript">
function MM_jumpMenu(targ,selObj,restore)
{
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function ClearFields()
{
	document.all.type.options.length = 0;
	document.all.product.options.length = 0;
}

function ClearProduct()
{
	document.all.product.options.length = 0;
}

function SendQuery(ID, Method, oDestination)
{
 if(ID != -1)
 {
  var sReturn
  var oOptionArray,oItemArray
  
  var xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
  xmlHTTP.open("GET", "GetProduct.php?InOut=2&ID=" + ID + "&Method=" + Method, false);
  xmlHTTP.send()
  var xmlDOM = new ActiveXObject("Microsoft.XMLDOM");

  sReturn = xmlHTTP.ResponseText;

  oOptionArray = sReturn.split(";")
  oDestination.length = 0
  
  for(i=0; i < oOptionArray.length-1; i++)
  {
	oItemArray = oOptionArray[i].split(":")
	
	var newOption = document.createElement("OPTION");
	newOption.value = oItemArray[0];
	newOption.text = oItemArray[1];
	oDestination[i] = newOption;
  }

  //oDestination.selectedIndex = 0;
 }
}
</script>
I fiddled with parts of it. I really don't know too much about javascript. I'm more of a C++/PHP/SQL kinda guy, so it makes it easier to understand, but I still don't know all the functions and syntax and such. As for my second request, I was wondering how I can dynamically change an image on a page by clicking a value from a list? I'll have the file path to the image as the list value which I'll pull from a PHP/MySQL query, but I need to know how to use javascript to change an image on a page. We do need cross-browser compatability for all this, that's the difficult thing. That ActiveX script was nice, but it only works on IE I guess. =/ Thanks guys!

Share this post


Link to post
Share on other sites
To make it work on non-IE browsers, you have to change the way you construct XMLHttpRequest. I'm not sure what your variable "xmlDom" is for - you probably don't need it.

Typically I'd use some code which used feature-detection:


function CreateRequest() {
if (XMLHttpRequest) {
return new XMLHttpRequest();
}
if (ActiveXObject) {
try {
// The following may fail, even if ActiveXObject is defined, depending on browser
// settings etc.
return new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// oh dear.
return null;
}
}
}


Or something like that. In non-IE browsers, XMLHttpRequest is a real javascript object which can be constructed with "new". It behaves similarly to Microsoft's one (in most cases).

Mark

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this