Making AJAX return a value.

sonicsshadow

Member
Messages
200
Reaction score
0
Points
16
Hi, I'm trying to make it so that AJAX will return a value to javascript rather than just printing out stuff. Is this possible?
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
Of course, with the xml-object-thingie, you get regular text, and you're not forced to do anything with it.
Something like "var something = yourxmlrequest.responseText;"
 

VPmase

New Member
Messages
914
Reaction score
1
Points
0
Hi, I'm trying to make it so that AJAX will return a value to javascript rather than just printing out stuff. Is this possible?
I don't think you'd be able to do something like:
PHP:
return "Test"
for AJAX. You have to echo/print what you want to return.
 

sonicsshadow

Member
Messages
200
Reaction score
0
Points
16
Your website's sessions are messed up, I login, go to that page, it prompts me to login again, takes me back to the index, I type in that url again and repeat.
 

mattura

Member
Messages
570
Reaction score
2
Points
18
Your AJAX needs to echo the value:
PHP:
if ($_REQUEST['ajax']=="getValue") {echo $value;exit;}
and your javascript need to catch it:
PHP:
//xmlHttp = new ajax object (browser specific code/try catch here)
xmlHttp.onreadystatechange=function() { 
 if(xmlHttp.readyState==4) {
  if (xmlHttp.status==200) {
    doSomethingWithThisValue(xmlHttp.responseText);
  }
 }
}
//send the request
  xmlHttp.open("POST", "http://www.thispage.php", true);
  xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", params.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send("ajax=getValue");
 
Top