Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

accessing http.responseText


3:01 pm on Apr 8, 2007 (gmt 0)

5+ Year Member

I have a php webpage which via an xmlhttprequest, executes a php script. This script queries a database and returns the results. At the moment my query creates two arrays, one a single array e.g.
$array[], and a 2-dimensional array e.g. $array[][]. I have alerts monotoring the http.readystate and htp.status. In windows I am getting the readystate alerts 1,2,3,4 and status 200 and can view the responseText in an alert. I am trying to extract the arrays from responseText. I tried using

result = http.responseText;
var result = result.split(",");

when I view result[0] via an alert, it just shows code from my php script. Can someone advise me on this?

My other problem is that in tomcat, the request only gets as far as the first alert, readystate 1, then nothing. The firewall is turned off. If anyone has an idea what may be causing this, please shout.

function Parts()
var url="http://localhost:8080/Databases/parts.php";
http = new XMLHttpRequest();
else if (window.ActiveXObject)
http = new ActiveXObject(Microsoft.XMLHTTP);
http.onreadystatechange = function()
if(http.readyState == 4)
if(http.status == 200)
result = http.responseText;
var data = result.split(",");
alert("No can do!");
http.open("GET", url, true);

Thanks in advance,


6:34 pm on Apr 8, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

It sounds to me like it's the php end that needs the work - is it basically dumping the array variables?
If so, instead of returning a php array, I'd either format it with delimiters that the javascript side can split on or form it as something that javascript can eval - I'm weak on javascript so I don't know if you could return something like "var rtn1=Array(...)" or not.

I'd be more likely to do it with delimiters - something like
$returnstring = '';
foreach($array as $val)
$returnstring .= $val . '*';
$returnstring = substr($returnstring,0,-1);
echo $returnstring;
Then you could split on * in the javascript. Since you're returning some complex structure, you could separate the two arrays with one delimiter, the two dimensions of the second array with another delimiter, and finally the individual elements with a third delimiter.

8:38 pm on Apr 13, 2007 (gmt 0)

5+ Year Member

I thought I had managed to split the two array as such, getting no error at least

var array = http.responseText;
if (array == false)
ans = 'invalid entry';
var fields = array.split("[],");
var parts = array.split(",[][]");

but when I typed


instead of getting something that resembled an array, I got all the code from my php script.

also when I tried to split the arrays themselves

var fieldnames = fields.split(",,,,");
var parts = parts.split("[],[]");

I got object doesn't support this method or property. Why can I have my cake and eat it?


9:12 am on Apr 14, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

I got all the code from my php script

Literally? Like:
// File to serve ajax request
include "dbstuff.php";

for($i=0; $i<10;$i++) {
$array[] = mysql_fetch_array($query);

If so then the server isn't processing the script for some reason. I'm not familiar with this:
Is it possible that that line is causing the server to send the file back instead of executing the contents - what happens if you comment that out?

8:43 pm on May 1, 2007 (gmt 0)

5+ Year Member

I have my php working fine. I am trying to parse responseText using split("¦") and split(","). I want to split the response which contains 2 variables and 2 array. Firstly I am using split("¦") to split the response into 4 elements such as
(numrows¦numfields¦fieldname[]¦parts[][]) and then use split(",") to split the 2 arrays.

result = http.responseText.split("¦");

this alert dsiplays all the elements ok

numrows = result[0];

displays all the data as well. Can anyone tell me where I am going wrong?

Featured Threads

Hot Threads This Week

Hot Threads This Month