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; PHP: and this line document.getElementById("content").innerHTML=http_request.responseText; PHP: 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; PHP: 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.'); } } } PHP:
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.'); } } } PHP:
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.'); } Code (markup):
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.
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; }); PHP:
Oops, small error, the last three lines should look like this: menuRequest(19, function(response){ document.getElementById("content"+19).innerHTML=response; }); PHP: Also on line 3 replace window.ActiveX with window.ActiveXObject.
Ugh, I don't know whats wrong with me. 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; }); PHP: