Skip to main content
Participant
September 5, 2006
Question

AJAX Help needed for an AJAX noob

  • September 5, 2006
  • 4 replies
  • 337 views
Hey there,

I'm starting to experiment with AJAX for simple form validation but am having great difficulty in getting the following working.

I'm trying to get it to count the length of a field, and respond back to an id'd span. It works if I hard code the span id into the JS file, but not if I try and implement a variable to have the span id passed into.

I have attached the code. If someone could take a look and point out where I'm going wrong, that would be greatly appreciated.

Cheers in advance,
Brad
This topic has been closed for replies.

4 replies

Inspiring
September 5, 2006
> In the mean time, if anyone can point out why passing the span id to the
> stateChanged function causes the xmlHttp.readyState to always stay at 0,
> that
> would be very useful!

You can't pass an argument to a callback function like that. You can create
your own callback function and set it up inside a calling function so that
the argument is not lost:

var xmlHttp

function processRemote(callback, str) {
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert ('Browser does not support HTTP Request');
return;
}

var url='/includes/ajax/ajax_validate.asp';
url=url+"?val="+str;
url=url+"&sid="+Math.random();
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=function() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
var temp = new Array();
temp.push(xmlHttp.responseText);
callback.apply(callback,temp);
}
}
xmlHttp.send(null);
}



function GetXmlHttpObject() {
var objXMLHttp=null;
if (window.XMLHttpRequest) {
objXMLHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject) {
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}

function checkElement(str, spanid) {
if (str.length==0) {
document.getElementById(spanid).innerHTML="";
return;
}
var callback = function(str) {
document.getElementById(spanid).innerHTML=str;
}
var result = processRemote(callback, str);
}


That should work.


--
--
Tom Muck
co-author Dreamweaver MX 2004: The Complete Reference
http://www.tom-muck.com/

Cartweaver Development Team
http://www.cartweaver.com

Extending Knowledge Daily
http://www.communitymx.com/


Participant
September 5, 2006
Thanks for the responses thus far chaps.

I will look at Prototype but unfortunately with Spry being pre-release, I won't be allowed to use it (it's an application I'm developing for work and no un-supported/pre-release technologies are allowed unfortunately).

I've already caused enough ripples by insisting my team switch over to Studio 8 from Visual Studio and (shudder) Frontpage... hehe.

In the mean time, if anyone can point out why passing the span id to the stateChanged function causes the xmlHttp.readyState to always stay at 0, that would be very useful!

Cheers,
Brad
Inspiring
September 5, 2006
Take a look at the SPRY framework for AJAX, available on Adobe Labs

http://labs.adobe.com/technologies/spry/

Cool, I repeat very cool. You can bet support for this will be included in
future versions of Dreamweaver, so it's definitely worth getting familiar
with.

--
Lawrence Cramer
*Adobe Community Expert* - Dreamweaver - http://tinyurl.com/jhnyq
email: lawrence at cartweaver dot com

Cartweaver CF, ASP & PHP Shopping Cart for Dreamweaver
www.cartweaver.com
news://support.cartweaver.com
=====================================================




"digitalus media" <webforumsuser@macromedia.com> wrote in message
news:edk7t5$7bl$1@forums.macromedia.com...
>i would suggest looking at prototype. it makes ajax real easy.


September 5, 2006
i would suggest looking at prototype. it makes ajax real easy.