Jump to content

I am getting somewhere lol


user4fun

Recommended Posts

terms.phpcombo box populated from a tablesend value to get_def.phpget_def.phpwould generate $found_def$found_def would be send back to terms.phptext area in terms.php would be updatedhere is what I haveterms.php

<?include ("inc/main_hdr.php");?><script type="text/javascript">   var xmlHttp;function init_ajax(){  try  {	xmlHttp=new XMLHttpRequest();  }  catch (e)  {	try	{	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");	}	catch (e)	{	  try	  {		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");	  }	  catch (e)	  {		alert("Your browser does not support AJAX!");	  }	}  }} function changetext()		{		document.getElementById('text').value = "Loading...";		init_ajax();		var picked = document.getElementById('choice').value;		 		xmlHttp = new XMLHttpRequest;		xmlHttp.onreadystatechange = function() {			if (xmlHttp.readyState == 4)			document.getElementById('text').value = xmlHttp.responseText;		}		xmlHttp.open("GET","get_def.php?choice=" + picked, true);	   xmlHttp.send(null);	}</script><form method="GET" name="term_definitions"><select name="choice"><? $link = mysql_connect("mysql", "", "") or die ("No connection");   mysql_select_db("Information") or die ("no database");   $result = mysql_query("SELECT ID, Term FROM Terms");	while ($row=mysql_fetch_array($result)) {		echo '<option value='.$row['ID'].'>'.$row['Term'].'</option>\n';	}?></select><input type="submit" name="find" value="search" onclick="changetext();"></form><textarea id="text"></textarea></body></html>

fileget_def.php

<?$picked_def = $_REQUEST['choice']; $link = mysql_connect("mysql", "", "") or die ("No connection");   mysql_select_db("Information") or die ("no database"); $result = mysql_query("SELECT * FROM Terms WHERE Term = '$picked_def'");$row = mysql_fetch_array($result);	echo $row['ID'] . " " . $row['Definition'];  $found_def = $row['Definition'];	  ?>

I am lost on where to go from there?The text area is not populating with the definition and the combo box is restarting at the first choice in the list?

Link to comment
Share on other sites

I think the problem is you're clicking a submit button, and the form gets submitted (and thus resetted). You need to return false to prevent that from happening. Ideally onsubmit:

document.forms[0].onsubmit = function() {return false;}

But really... you have one VERY inefficient code. Try...catch is a very expensive process in JavaScript, and you're using it for something where you can easily avoid it. Here's a whole new version that avoids this, and is more compact too:

<script type="text/javascript">   var xmlHttp = (window.XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));function changetext() {		if (!xmlHttp) { return true;}		document.getElementById('text').value = "Loading...";		var picked = document.getElementById('choice').value;		xmlHttp.onreadystatechange = function() {				if (xmlHttp.readyState == 4) {					document.getElementById('text').value = xmlHttp.responseText;				}		}		xmlHttp.open("GET","get_def.php?choice=" + picked, true);		xmlHttp.send(null);		return false;}</script>

If the user doesn't have support for XHR, they are going to submit the form. I'm not sure if the last line would work, or break things. Remove it if it doesn't work.

Link to comment
Share on other sites

I don't think that is entirely correct, it doesn't account for the MSXML2 version of the object. It doesn't take a lot of time to run that code, someone could time it if they were interested in determining the speed difference but the try..catch block accounts for all possibilities, the if statement doesn't. Also, you can delete this line:init_ajax();var picked = document.getElementById('choice').value; xmlHttp = new XMLHttpRequest;xmlHttp.onreadystatechange = function() {The init_ajax function creates the object, you don't need to explicitly do it again.

Link to comment
Share on other sites

I don't think that is entirely correct, it doesn't account for the MSXML2 version of the object. It doesn't take a lot of time to run that code, someone could time it if they were interested in determining the speed difference but the try..catch block accounts for all possibilities, the if statement doesn't.
Isn't the Microsoft.XMLHTTP version the oldest, and thus most likely to be available version of XHR? Why at all consider MSXML2, especially with IE7's "native" XHR ability?
Link to comment
Share on other sites

Why at all consider MSXML2
For no reason other then someone might have it. IE7 is the most recent version of IE but look at the numbers there. There's no guarantee that the user has had their XML library upgraded to use the newer version, and it's really easy to check for (with the try..catch block).
Link to comment
Share on other sites

For no reason other then someone might have it. IE7 is the most recent version of IE but look at the numbers there. There's no guarantee that the user has had their XML library upgraded to use the newer version, and it's really easy to check for (with the try..catch block).
My point exactly. IE7 has support for XMLHttpRequest(), while older IEs are sure to have ActiveXObject("Microsoft.XMLHTTP"), but are not sure to have ActiveXObject("Msxml2.XMLHTTP"). All performance tests I've seen (like Opera's Efficient JavaScript, RockStarApps and others) show try...catch as an expensive construct, and considering MSXML2 is useless anyway.
Link to comment
Share on other sites

Well, you're welcome to do whatever you want but I don't think that taking a few extra milliseconds to potentially support a wider audience is a bad tradeoff. We had someone on here a few weeks ago asking questions who was using IE4.

Link to comment
Share on other sites

Well, you're welcome to do whatever you want but I don't think that taking a few extra milliseconds to potentially support a wider audience is a bad tradeoff. We had someone on here a few weeks ago asking questions who was using IE4.
Yeah, I remember that guy :) . And if he has any xmlHttpRequest() support, it's going to be in the form of ActiveXObject("Microsoft.XMLHTTP"), correct?Of course we are all free to do whatever we want. I just wanted to see the goods and the bads of what I am/prefer doing. Having my assumptions judged is a good way to go "forward" :) .
Link to comment
Share on other sites

The w3schools AJAX page says this:

If that fails, try xmlHttp=new ActiveXObject("Msxml2.XMLHTTP") which is for Internet Explorer 6.0+, if that also fails, try xmlHttp=new ActiveXObject("Microsoft.XMLHTTP") which is for Internet Explorer 5.5+
Are we confused yet? It's not real easy to find a definitive guide on this MSXML business. I've found this:
Use MSXML 6.0 - it is "in the box" on Vista and available for download on Win2k, XP, and 2003. It has the best security, performance, reliability, and W3C conformanceMSXML 3.0 is our preferred "fallback" - It is installed on every OS from a fully patched Win2k SP4 installation on up, so it requires "zero-deployment" and is serviced regularly with the OSMSXML 4.0 was released to the web about 5 years ago, but at this point has been superseded by MSXML 6.0 and is only intended to support legacy applicationsMSXML 5.0 for Microsoft Office Applications is purpose-built for Office applications and isn't intended for broad deployment. Internet Explorer 7 actually has the MSXML5 components "off-by-default" in the Internet zone so your customers will get a goldbar for each MSXML5 control on a page if your code tries to instantiate it. The best recommendation is to avoid MSXML5 in your web apps (only machines with Office 2003 or higher will have it, anyway.).
No mention about MSXML 2.0. Which sort of makes me think that the 2 in MSXML2 is not a version number, just a namespace. Which adds some more confusion. It's not easy to find out which situations will require using the MSXML2 namespace, i.e. which combinations of versions of which products support that. So let's add a little bit more confusion.
MSXML2 vs. Microsoft namespace - I've also seen a lot of code that instantiates the "Microsoft.XMLHTTP" ActiveX object rather than the MSXML2.XMLHTTP.3.0 or MSXML2.XMLHTTP.6.0 if you're using 6.0. The "Microsoft" namespace is actually older and is only implemented in MSXML3 for legacy support. It's unfortunate we used the "better" name on the older version, but stick to the "msxml2" namespace when instantiating objects.
Clear as mud? This is worth discussing though, I'm checking some more links for information.Here's another link from an IE program manager:http://blogs.msdn.com/ie/archive/2006/01/23/516393.aspxThis page shows that there are a ton of MSXML versions that get distributed with a ton of different applications:http://support.microsoft.com/kb/269238No mention of the msxml2 namespace there.Are all you kids paying attention out there? This is why you don't integrate the browser with the operating system.
Link to comment
Share on other sites

try xmlHttp=new ActiveXObject("Microsoft.XMLHTTP") which is for Internet Explorer 5.5+
So does the "+" means this one is available on IE6 too (even though it's just for legacy purposes)? If so, then bottom line remains that there is no point in supporting MSXML2.I think people once used it because there was no indication MS was going to ever support xmlHttpRequest(), but there was indication that Microsoft.XMLHTTP may get removed in favor in MSXML2.XMLHTTP. Nowadays, if there's an indication that anything would be removed in the future, it's going to be both Microsoft.XMLHTTP and MSXML2.XMLHTTP, since neither are needed with the native xmlHttpReqeust() support.
Link to comment
Share on other sites

Right, IE7 supports the window.XMLHTTPRequest object like everything else. But I still can't find a real definitive answer for whether or not to use the Microsoft or MSXML2 namespace, the things I've seen seem to conflict with each other about which one to use. Apparently MSXML2 is more recent, but then the IE guy shows using the Microsoft namespace.

Link to comment
Share on other sites

Right, IE7 supports the window.XMLHTTPRequest object like everything else. But I still can't find a real definitive answer for whether or not to use the Microsoft or MSXML2 namespace, the things I've seen seem to conflict with each other about which one to use. Apparently MSXML2 is more recent, but then the IE guy shows using the Microsoft namespace.
I don't know about you, but the real question for me is "what does the MSXML2.XMLHTTP offer that Microsoft.XMLHTTP doesn't?". As far as I'm concerned, it offers nothing Microsoft.XMLHTTP doesn't have, and since Microsoft.XMLHTTP is older and available for both newer and older IEs, I see it as the defacto choise.
Link to comment
Share on other sites

This is the point I'm trying to determine - I can't find anything online with regard to which objects are available in which versions of anything.
Unfortunatly, excluding the W3Schools note you quoted above, I haven't found any such information either. My assumptions about Microsoft.XMLHTTP and MSXML2.XMLHTTP are based solely on the above note. I've never tried XHR with IE6, let alone IE5.5. Well, on the bright side, as IE7 grows, this is going to become less and less of a problem.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...