Car Accident Lawyer Los Angeles - Xbox Mod Chip - Car Insurance - Free Ringtones - Mobile Phone deals

PDA

View Full Version : Ajax , php Question


mohajer
Jan 2nd 2008, 11:22 am
I use Ajax to Update And Then show info
language is PHP , ajax access content.php?id=$id
to do something ,

this line is contain id :
url = "content.php?id=" + url;
and this line
document.getElementById("content").innerHTML=http_request.responseText;
content is name of a div that will a place for show result.
but i want to add id to ("content")
for example ("content+$id")
then if content.php?id=23 then we have :
document.getElementById("content23").innerHTML=http_request.responseText;

function menuRequest(url)
{
var arg = url;
url = "content.php?id=" + url;
var http_request = false;

if (window.XMLHttpRequest)
{ // Mozilla, Safari,...
http_request = new XMLHttpRequest();

if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
// See note below about this line
}
} else if (window.ActiveXObject)
{ // IE
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!http_request)
{
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}

http_request.onreadystatechange = function() { alertContents(http_request); };
http_request.open('GET', url, true);
http_request.send(null);

}


function alertContents(http_request)
{
if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
document.getElementById("content").innerHTML=http_request.responseText;
}
else
{
alert('There was a problem with the request.');
}
}
}

coderbari
Jan 2nd 2008, 11:38 am
It may help.I did some changes.
function menuRequest(id)
{

url = "content.php?id=" + id;
var http_request = false;

if (window.XMLHttpRequest)
{ // Mozilla, Safari,...
http_request = new XMLHttpRequest();

if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
// See note below about this line
}
} else if (window.ActiveXObject)
{ // IE
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!http_request)
{
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}

http_request.onreadystatechange = function() { alertContents(http_request); };
http_request.open('GET', url, true);
http_request.send(null);

}


function alertContents(http_request)
{
if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
document.getElementById("content"+id).innerHTML=http_request.responseText;
}
else
{
alert('There was a problem with the request.');
}
}
}

mohajer
Jan 2nd 2008, 12:44 pm
thanks for reply but seems not work.

Kaizoku
Jan 2nd 2008, 4:46 pm
Simplify things abit, don't need curly braces if only 1 argument.

function alertContents(http_request) {
if (http_request.readyState == 4 && http_request.status == 200)
document.getElementById("content"+id).innerHTML=http_request.responseText;
else
alert('There was a problem with the request.');
}

mohajer
Jan 3rd 2008, 1:13 am
not work ,
when i use manual edit it work fine for example ("content19") will work (result will show in div id=23)
but "content"+id) not work.

however also when i try document.getElementById(id) again not work.

mohajer
Jan 3rd 2008, 9:39 am
any suggestion ?

MMJ
Jan 3rd 2008, 11:38 am
This should work but I didn't try it. Remember, the less code there is the less chance of bugs.

function menuRequest(id, handler){
var url = "content.php?id=" + id;
var xhr = (window.ActiveX ? new ActiveXObject("MSXML2.XMLHTTP") : new XMLHttpRequest()) || alert('Giving up :( Cannot create an XMLHTTP instance');

xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.statusText == 'OK')
handler(xhr.responseText);
else
alert('There was a problem with the request.');
};
xhr.open('GET', url, true);
xhr.send(null);
}

menuRequest(19, function(response){
document.getElementById("content"+id).innerHTML=response;
});

MMJ
Jan 4th 2008, 1:31 am
Oops, small error, the last three lines should look like this:

menuRequest(19, function(response){
document.getElementById("content"+19).innerHTML=response;
});

Also on line 3 replace window.ActiveX with window.ActiveXObject.

MMJ
Jan 4th 2008, 7:39 am
Ugh, I don't know whats wrong with me. :o

Anyways, use this instead of the above:

function menuRequest(id, handler){
var url = "content.php?id=" + id;
var xhr = (window.ActiveXObject ? new ActiveXObject("MSXML2.XMLHTTP") : new XMLHttpRequest()) || alert('Giving up :( Cannot create an XMLHTTP instance');

xhr.onreadystatechange = function() {
if (xhr.readyState == 4)
if (xhr.statusText == 'OK')
handler(xhr.responseText, id);
else
alert('There was a problem with the request.');
};
xhr.open('GET', url, true);
xhr.send(null);
}

menuRequest(19, function(response, id){
document.getElementById("content"+id).innerHTML=response;
});