Thread: AJAX Noob
View Single Post
Old 02-19-2008, 09:38 PM   PM User | #5
alexharvey_eu
New to the CF scene

 
Join Date: Feb 2008
Location: Manchester, UK
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
alexharvey_eu is an unknown quantity at this point
i was using this to post name-value pairs to a php script

you can add onblur="ajaxFunction(this.id,this.value)" to your forms input fields so they can validate in real time. I had div elements with id's the same as their corrosponding form field but with an x infront (i read somewhere not to give things the same id). hope this helps anyone...

Code:
function ajaxFunction(id,value){
	var ajaxRequest;//ajax var
	
	try{// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	}catch(e){//Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){// Something went wrong
				return false;
			}
		}
	}
	
	ajaxRequest.onreadystatechange = function(){//handle response
		if(ajaxRequest.readyState == 4){
			document.getElementById("x"+id).innerHTML = ajaxRequest.responseText;
		}
	}
	var infoToSend = encodeURI("id="+id+"&value="+value);
	ajaxRequest.open("POST", "server.php", true);
	ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxRequest.setRequestHeader("Content-length", infoToSend.length);
	ajaxRequest.setRequestHeader("Connection", "close");
	ajaxRequest.send(infoToSend); 
}
alexharvey_eu is offline   Reply With Quote