duncanhall Posted September 25, 2006 Share Posted September 25, 2006 I'm trying to experiment a little with AJAX, but don't have a lot javaScript experience, so any help on this would be very much appreciated.I have my javaScript, which instantiates the [i]XMLHttpRequestObject[/i], and I then use the [i]open[/i] method of this object to open a PHP script to do some processes. At the moment, the PHP simply returns a string and this is then handled by the javascript to update a string on the page.Pretty simple, and working fine. But does anyone have any help as to how I can return an array from the PHP, rather than just a string, and then traverse this array accordingly in the javaScript? Quote Link to comment https://forums.phpfreaks.com/topic/21962-php-ajax-help/ Share on other sites More sharing options...
yonta Posted September 25, 2006 Share Posted September 25, 2006 You could echo the array as xml and then let javascript traverse the xml. Check this article http://www.sitepoint.com/article/xml-data-traversal or this one http://www.devx.com/xml/Article/11866 . Quote Link to comment https://forums.phpfreaks.com/topic/21962-php-ajax-help/#findComment-98106 Share on other sites More sharing options...
ober Posted September 25, 2006 Share Posted September 25, 2006 I'm not exactly sure why you would want to do this, unless the array needs some further interaction on the client side. You can do your processing of the array in PHP before you pass it back to the page. You can then pass it back and forth using AJAX instead of doing all the processing on the JS side if you wish. And if your JS knowledge isn't that great, that might be the way to go. Quote Link to comment https://forums.phpfreaks.com/topic/21962-php-ajax-help/#findComment-98113 Share on other sites More sharing options...
duncanhall Posted September 25, 2006 Author Share Posted September 25, 2006 Thanks for the responses, I have found a temporary solution that works satisfactorily, but for future purposes, perhaps I should ellaborate on the problem.I have two text links. When each one is clicked, I want the PHP/JavaScript combination to return two distinct values. i.e:click link 1: return stringA and stringBclick link 2: return stringX and stringYUsing my current method, the value returned by the PHP script is fed back to the javaScript as [i]XMLHttpRequest.responseText[/i]. This means I can only access the returned value as a whole, not as two seperate strings.Currently, I have solved the problem by concatenating the two strings in PHP and returning them as one long string. I then used the javaScript [i]slice[/i] method to split them into two seperate strings.Alhough this works, I thought It might be possible to pass an array from the PHP to the javaScript so that I could have two wholly seperate variables.Any comments? Quote Link to comment https://forums.phpfreaks.com/topic/21962-php-ajax-help/#findComment-98182 Share on other sites More sharing options...
ober Posted September 25, 2006 Share Posted September 25, 2006 Well... if it's a formatting issue, you can format it in the response before ever sending it back. You do know that, right? Are you doing any actual work with the data once you get it back to the page? Quote Link to comment https://forums.phpfreaks.com/topic/21962-php-ajax-help/#findComment-98201 Share on other sites More sharing options...
duncanhall Posted September 25, 2006 Author Share Posted September 25, 2006 There's no formatting issues, the string is presented in the PHP and returned just as I want it. There's no real 'work' being done to the data once it's returned. It's simply fed into a page element as below:[code] var response = http.responseText; if(response) { document.getElementById("small").innerHTML = response.slice(0, 54); document.getElementById("large").innerHTML = response.slice(55, 109); }[/code]whereas what I wanted to do was something like:[code] var response = http.responseText; if(response) { document.getElementById("small").innerHTML = response[0]; document.getElementById("large").innerHTML = response[1]; }[/code]Looking at it, I guess it doesn't really make a lot of difference, and as I said, it's doing everything I need it to. It's just I only started 'learning AJAX' about 5 hours ago, and I haven't been going at PHP for all that long either, so I don't want to get into bad habits early on. Quote Link to comment https://forums.phpfreaks.com/topic/21962-php-ajax-help/#findComment-98217 Share on other sites More sharing options...
ober Posted September 25, 2006 Share Posted September 25, 2006 Well, you have to remember that you can't pass arrays between JS and PHP. JS cannot reference a server-side array and vice versa.I didn't realize you were trying to put the response into 2 different areas. That info would have been useful in my response. Quote Link to comment https://forums.phpfreaks.com/topic/21962-php-ajax-help/#findComment-98225 Share on other sites More sharing options...
duncanhall Posted September 25, 2006 Author Share Posted September 25, 2006 Sorry, poor explanations on my part. I have trouble understanding exactly what's going on myself at times.Right, assuming I'm happy with my current methodology (at least for now), I have a slightly different question:After reading several other threads, it appears there's a discrepancy over using session variables. Will this be a problem for me if I just use the php script I already have to create a session variable. i.e.[list][*]User clicks link.[*]JavaScript is processed.[*]PHP script is run, which returns the strings as before but now also sets a session variable.[*]JavaScript Response handler is processed.[/list]Will this session variable remain set regardless of refreshes or site navigation? Quote Link to comment https://forums.phpfreaks.com/topic/21962-php-ajax-help/#findComment-98236 Share on other sites More sharing options...
ober Posted September 25, 2006 Share Posted September 25, 2006 meh... my counterpart on this issue probably has a better response. I think it's a bad idea to use sessions in conjunction with AJAX, but whatever. Quote Link to comment https://forums.phpfreaks.com/topic/21962-php-ajax-help/#findComment-98264 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.