grailkeeper
09-15-2008, 09:29 PM
Hi there,
I've been looking thru many pages for a way to improve my basic post request AJAX code to allow it to target different modules of a page on the same click.
Let me explain with some code:
function get_tag(obj) {
var etiquetadiv = "ajax_tags";
var poststr = "tag=" + encodeURI( document.getElementById("tag").value );
tag.value="";
makePOSTRequest('enviapost.php?pag=tag', poststr, etiquetadiv);
}
This code launches the typical basic ajax "makepostrequest" function, but you can see i've modified it adding a var "etiquetadiv" that lets me have many functions for every module I want to use.
Here is the code of makepostrequest, if someone needs it here:
var http_request = false;
function makePOSTRequest(url, parameters, etiquetadiv) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} 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('No s\'ha pogut crear la instància XMLHTTP');
return false;
}
http_request.onreadystatechange = function(){
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById(etiquetadiv).innerHTML = result;
} else {
alert('No s\'ha carregat correctament el fitxer.');
}
}
};
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
In my page, i've got 2 tag modules: One for the whole list, and a little one that shows only the last 5 tags.
So, what I would really like to do... is to update the list AND the the module at the same time.
I've been trying with adding a line makePOSTRequest('enviapost.php?pag=tagmenu', '', 'menu_div'); in the get_tag function... but then it seems to just refresh the last div call.
Could you give a hand with this?
T Y so much! ;)
I've been looking thru many pages for a way to improve my basic post request AJAX code to allow it to target different modules of a page on the same click.
Let me explain with some code:
function get_tag(obj) {
var etiquetadiv = "ajax_tags";
var poststr = "tag=" + encodeURI( document.getElementById("tag").value );
tag.value="";
makePOSTRequest('enviapost.php?pag=tag', poststr, etiquetadiv);
}
This code launches the typical basic ajax "makepostrequest" function, but you can see i've modified it adding a var "etiquetadiv" that lets me have many functions for every module I want to use.
Here is the code of makepostrequest, if someone needs it here:
var http_request = false;
function makePOSTRequest(url, parameters, etiquetadiv) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} 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('No s\'ha pogut crear la instància XMLHTTP');
return false;
}
http_request.onreadystatechange = function(){
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById(etiquetadiv).innerHTML = result;
} else {
alert('No s\'ha carregat correctament el fitxer.');
}
}
};
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
In my page, i've got 2 tag modules: One for the whole list, and a little one that shows only the last 5 tags.
So, what I would really like to do... is to update the list AND the the module at the same time.
I've been trying with adding a line makePOSTRequest('enviapost.php?pag=tagmenu', '', 'menu_div'); in the get_tag function... but then it seems to just refresh the last div call.
Could you give a hand with this?
T Y so much! ;)