PDA

View Full Version : AJAX includes unable to pass HTTP query via GET method!



JAB Creations
12-31-2007, 06:15 AM
Below is the AJAX script and an example function. The function works with the HTTP query though it's not passed from the AJAX script. The script itself merely loads XHTML content from a file on the server in to an element (in the example function below it's loaded in to the element with the id of promptsajax). I have to admit that fixing this script is a bit over my head. :confused:

AJAX Function Example

ajaxpage('includes/contact.php?contact=frank', 'promptsajax');

AJAX Includes Script

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}

XAKERA
12-31-2007, 06:59 AM
u mean that u can't add new ajax mod in this content?

codeexploiter
12-31-2007, 08:36 AM
Can you provide a link to your page?

JAB Creations
12-31-2007, 11:06 AM
My site is http://www.jabcreations.com/

The AJAX script is what I use to load XHTML in to the prompt (click on site options menu (once inside look at the top right) and that layer is what I refer to as the prompt). If you watch closely at the status bar for the anchors when you tab through them you'll see those anchors are all JavaScript functions that essentially keep calling different AJAX pages in to the prompt. It's one of the ways I've minimized bandwidth. :D

JAB Creations
12-31-2007, 11:11 AM
u mean that u can't add new ajax mod in this content?

I can include a page like page.php but not page.php?http_query_property=http_query_value. If I do that it gets lost in the script, but it would work fine if I didn't use AJAX...but that defeats the purpose of using AJAX! I haven't found anything online ... using my best understanding of the terminology though I am not advanced enough to also understand how to necessarily approach this to the extent of making an educated guess of what (JavaScript) else I could search for in the search engines in hopes of making a lucky find.

jscheuer1
12-31-2007, 11:45 AM
Oddly enough:



page.php?http_query_property=http_query_value

Would probably be fine, literally. But, I'm pretty sure that you mean something like:


page.php?page=http://www.jabcreations.com/thispage.php?bob=apples

If so, you could do:


page.php?page=http%3A//www.jabcreations.com/thispage.php%3Fbob%3Dapples

If anything would work here, that should. That is, if I've understood the problem.

JAB Creations
12-31-2007, 11:54 AM
Well your suggestions gave me the idea to try loading the page via it's absolute path (which defeats the purpose of local testing) but it still did not work...not sure what your other suggestions were based on though?

The script needs to somehow be modded to allow HTTP queries and I'm not that JavaScript savvy just yet.

So this looks like regex in JavaScript simpleton terms, does anything here look counter-productive?


bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()

jscheuer1
12-31-2007, 03:08 PM
The bustcacheparameter will add a query variable with a unique value to the URL. It shouldn't affect any existing query string as it will add this using the & character if a query string already exists. Since you are having problems in this area though, and this does affect the query string, you could always disable it to see if that helps:


var bustcachevar=0 //bust potential caching of external pages after initial request? (1=yes, 0=no)

However, I believe the correct approach is to escape the query string as indicted in my previous post. You need not use the absolute path, but the string should be escaped.

Disabling bustcacheparameter and escaping, or just disabling bustcacheparameter may be required, if either and you need the bustcacheparameter functionality, it could be rewritten to be inserted as the first query var/value pair in all cases if that would help. However, if bustcacheparameter creates a problem merely by being present, even on URL's with simple existing queries, there is something wrong with the 'receiving' PHP.

JAB Creations
12-31-2007, 03:19 PM
It would help if people kept the script's credits in tact! :mad:

So I did a Google Search (http://www.google.com/search?hl=en&q=bustcacheparameter&btnG=Google+Search) and found a page giving credit to this script...only it included the credits!

My interest has started to include this script now though it lacks the ability to target an ID (from a casual look) and I *just* got a reply as I'm writing this...
http://www.dynamicdrive.com/dynamicindex17/ajaxroutine.htm

Here is my receiving PHP...

<?php if (isset($_GET['name'])) {echo 'name_is_' . $_GET['name'];}
else {echo 'not set<br />';}
?>

I'm no PHP guru though I don't necessarily see anything wrong with that. I've set the bustcacheparameter to 0 and escaped the HTTP query parameter as so...

ajaxpage('includes/contact.php\?nameis=john', 'promptsajax');

I'm happy to at least know where the script is from now! I'm checking out the other examples...I really hope I can get this to work! Thanks for posting with such great timing! :)

jscheuer1
12-31-2007, 03:36 PM
That receiving PHP is looking for 'name'. You are sending it 'nameis'. It will never find that.

And, that's just a simple query and shouldn't even need escaping. Escaping is only for those query values that include special URL characters, as in the example from my first post in this thread where I asked if that was the problem.

Try:


<?php if (isset($_GET['nameis'])) {echo 'name_is_' . $_GET['nameis'];}
else {echo 'not set<br />';}
?>

And make sure that part works even when not using Ajax.

JAB Creations
12-31-2007, 03:45 PM
Fixed that small error, a direct request with the HTTP query works now but only on the direct request...I get the query property instead of it's value...but it's something rather then nothing.

It's still not receiving any GET queries regardless of any of the combinations of escaping the question mark or setting the cache to 0 or 1. I've tried merging some of the code from the other script though that hasn't done anything useful either. :confused:

jscheuer1
12-31-2007, 03:54 PM
I never said to escape the question mark, only URL components in the query value. A query string is made up of four parts:


? (or & if more than one and this one isn't the first)

the_name_of_the_variable

=

the_value


Only the value need be escaped here, and only if it contains special URL characters. And it should be escaped using the the escape codes, as I originally indicated, not the \ slash.

JAB Creations
12-31-2007, 04:05 PM
Only the value need be escaped here ... And it should be escaped using the the escape codes, as I originally indicated, not the \ slash.

I think I am getting what you're saying, conceptual wise at least. I don't need to escape the value in the mini-script function calling the main function. I need to somehow escape the HTTP query-value but inside of the script itself. That is something I might (with lots of luck) adapt from the other script I posted a link to but I have no other method of figuring out how to escape it? For the hey of it I removed the value in the mini-function (I keep the AJAX script in a scripts file and functions that use those scripts separately) and it didn't do any good (the non-AJAX version worked the same as expected).

jscheuer1
12-31-2007, 04:24 PM
I don't think it needs to be escaped at all, not that value. If you have the correct receiving PHP, it should work. I was just testing the source and it is still looking for 'name', not 'nameis'.

I also notice that the receiving field is hidden:


<input class="hidden" name="nameis" type="hidden" value="" />

So, even if this does work, it wouldn't be visible on the page.

Forget about Ajax for a moment, and make sure it works as expected using this URL typed into the browser's address field:


http://www.jabcreations.com/templates/ajax-site-contact-email.php?nameis=john

JAB Creations
12-31-2007, 04:57 PM
I try to make obvious variables as bland (and not live server exact) as possible. I haven't uploaded the files in question until I had the chance to read your last reply. I know that the input is hidden, it's the first time I've intentionally used it. The contact form will (once I get this test working) choose who the email is for...and a PHP array will handle the rest making a static contact form dynamic. :D I've updated the contact form's PHP to give a cleaner output on what it is receiving.

jscheuer1
12-31-2007, 05:05 PM
You have two function sitecontactshow() in your script:


function sitecontactshow() {
promptshow();
ajaxpage('templates/ajax-site-contact-email.php?nameis=john', 'promptsajax');
document.getElementById('promptsspan').innerHTML = 'Site Contact - E-Mail';
menusitecontact();
document.getElementById('menu_site_chatroom').focus();
}

and:


function sitecontactshow() {promptshow(); ajaxpage('templates/ajax-site-contact-email.php', 'promptsajax'); document.getElementById('promptsspan').innerHTML = 'Site Contact'; menusitecontact(); document.getElementById('menu_site_contact').focus();}


It is, of course, using the second one, the one with no query string.

JAB Creations
12-31-2007, 05:09 PM
:rolleyes: ...thanks. :D

Any and I mean any constructive criticisms, recommendations, and suggestions for cleaning up my JavaScript would be much appreciated! :o

jscheuer1
12-31-2007, 08:01 PM
Well, the code seems kinda messy to me, but I'm used to that. In helping out folks I see all sorts of coding styles and mishmashes of different coding styles. As a result, I tend to prefer a very bare style with minimal added tabs or spaces, with a certain format to functions and braced enclosures, and the omission of unneeded braces. I don't always get it exact, especially not when adding to other's code to help them.

Here is an example of my style (the code reports on a page's distribution of columns in its tables):


(function(){
var method=1; //use 1 to append report(s) to the bottom of the page, 0 for report(s) in alert(s)
for (var e = 0, tbls=document.getElementsByTagName('table'); e < tbls.length; e++){
var t=tbls[e], ec, cc, crpt=irpt='', rpt='Table #'+e+' cell report:\n', spr=function(){
var ec=0, n=i-1;
while(n+1){
for (var j = 0, c=t.rows[n].cells; j < c.length; j++)
if (c[j].getAttribute('rowspan',0))
ec=ec+(c[j].getAttribute('rowspan',0)-0>i-n? (c[j].getAttribute('colspan',0) ? c[j].getAttribute('colspan',0)-0 : 1) : 0);
n--
}
irpt=ec? ', from prior rowspan(s):'+ec : '';
return ec;
};
for (var i = 0, r=t.rows; i < r.length; i++){
ec=spr(), cc=0;
for (var j = 0, c=r[i].cells; j < c.length; j++)
if(c[j].getAttribute('colspan',0)&&c[j].getAttribute('colspan',0)-0>1){
ec+=c[j].getAttribute('colspan',0)-0;
cc+=c[j].getAttribute('colspan',0)-0;
}
else
ec++;
crpt=cc? ', from colspan(s):'+cc : '';
rpt+='\nrow#'+i+' cells='+ec+crpt+irpt;
}
var col=rpt.split('\n');
for (var z in col)
col[z]=col[z].length;
col.sort(function(a,b){return b-a;});
if(col[0]>23){
rpt+='\n\ncells= includes any \'from\' vals';
col.length+=2;
}
else
col.length++;
if(method){
var ta=document.createElement('textarea');
with(ta){
rows=col.length;
cols=col[0]+1;
with(style){
marginTop='1em';
overflow='hidden';
border='1px solid silver';
display='block';
position='relative';
zIndex=9999999999999;
fontFamily='monospace';
color='black';
backgroundColor='white';
}
value=rpt;
}
document.body.appendChild(ta);
if(ta.offsetHeight>(col.length+1)*16)
ta.rows--;
}
else
alert(rpt);
}
})();

But to clean up one's own code, one should decide on a style one likes and stick to it, or at least keep one's own code in it and follow the style of the author when adding small bits to scripts. Then there is the process of going over it to eliminate any unnecessary functions or variables, and making the scope of each object as limited as possible.

It is also a good idea to place comments anywhere that they might be useful in reminding you of a change you made, or of an important piece of the code whose purpose wouldn't otherwise be readily apparent to you when looking over it later - even much later. Comments generally should be more plentiful when the code is meant to be shared with others.