Jump to content

AJAX reuse xmlhttprequest object


wdean

Recommended Posts

I am experienced with PHP and JavaScript, but I'm new to AJAX. I'm having a bit of trouble getting this code to work right. It will run option 1 just fine, but it never makes it to the onstatechange function when I try option 2. What do I need to do to make this thing work?

 


function ajaxFunction(x)
{
var xmlhttp;
if (window.XMLHttpRequest)
{
	// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
	// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
	alert("Your browser does not support XMLHTTP!");
}

xmlhttp.open("POST",ajaxSelector(x),true);

xmlhttp.onreadystatechange=function()
{
	if(xmlhttp.readyState==4)
	{
	  ajaxResponse(x,xmlhttp.responseText);
	}
}

xmlhttp.send(null);
}

function ajaxSelector(x)
{
var page;

if (x == 1)
{
	page = "includes/header_panel/login_form.php";
}
else if (x == 2)
{
	page = "includes/scripts/login.php";
}

return page;
}

function ajaxResponse(x,str)
{
if (x == 1)
{
	var link = document.getElementById('loginLink');
	var headerboard = document.getElementById("headerboard");

	if (link.innerHTML == 'Login')
	{
		link.innerHTML = 'Close';
		headerboard.innerHTML = str;
	} else {
		link.innerHTML = 'Login';
		headerboard.innerHTML = '';
	}
}
else if (x == 2)
{
	document.getElementById('headerboard').innerHTML = 'test';
}
}

Link to comment
https://forums.phpfreaks.com/topic/165209-ajax-reuse-xmlhttprequest-object/
Share on other sites

Archived

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

×
×
  • 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.