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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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