| accessing http.responseText Ajax |
rdunne

msg:3305601 | 3:01 pm on Apr 8, 2007 (gmt 0) | 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(","); alert(result[0]);
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"; if(window.XMLHttpRequest) http = new XMLHttpRequest(); else if (window.ActiveXObject) http = new ActiveXObject(Microsoft.XMLHTTP); http.onreadystatechange = function() { alert(http.readyState); if(http.readyState == 4) { alert(http.status); if(http.status == 200) { result = http.responseText; var data = result.split(","); alert(result[0]); } else { alert("No can do!"); } } } http.open("GET", url, true); http.setRequestHeader("text"); http.send(null); }
Thanks in advance, RPJD :)
|
cameraman

msg:3305728 | 6:34 pm on Apr 8, 2007 (gmt 0) | 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 <?php $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.
|
rdunne

msg:3310671 | 8:38 pm on Apr 13, 2007 (gmt 0) | 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(",[][]"); alert("done");
but when I typed
alert(fields);
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? RPJD
|
cameraman

msg:3310980 | 9:12 am on Apr 14, 2007 (gmt 0) | | I got all the code from my php script |
| Literally? Like: <?php // File to serve ajax request include "dbstuff.php"; . . for($i=0; $i<10;$i++) { $array[] = mysql_fetch_array($query); } ?> etc.? If so then the server isn't processing the script for some reason. I'm not familiar with this: http.setRequestHeader("text"); 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?
|
rdunne

msg:3327851 | 8:43 pm on May 1, 2007 (gmt 0) | 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("¦"); alert(result);
this alert dsiplays all the elements ok but
numrows = result[0]; alert(numrows);
displays all the data as well. Can anyone tell me where I am going wrong?
|
|
|