Jump to content


Photo

PHP / AJAX Help?


  • Please log in to reply
8 replies to this topic

#1 duncanhall

duncanhall
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 25 September 2006 - 11:15 AM

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 XMLHttpRequestObject, and I then use the open 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?

#2 yonta

yonta
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 25 September 2006 - 12:33 PM

You could echo the array as xml and then let javascript traverse the xml. Check this article http://www.sitepoint...-data-traversal or this one http://www.devx.com/xml/Article/11866 .
do it, do it right, do it right now

#3 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 25 September 2006 - 01:03 PM

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.

Info: PHP Manual


#4 duncanhall

duncanhall
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 25 September 2006 - 02:17 PM

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 stringB

click link 2: return stringX and stringY


Using my current method, the value returned by the PHP script is fed back to the javaScript as XMLHttpRequest.responseText. 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 slice 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?

#5 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 25 September 2006 - 02:26 PM

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?

Info: PHP Manual


#6 duncanhall

duncanhall
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 25 September 2006 - 02:36 PM

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:

      
var response = http.responseText;
      if(response) {
         document.getElementById("small").innerHTML = response.slice(0, 54);
         document.getElementById("large").innerHTML = response.slice(55, 109);
      }

whereas what I wanted to do was something like:

      
var response = http.responseText;
      if(response) {
         document.getElementById("small").innerHTML = response[0];
         document.getElementById("large").innerHTML = response[1];
      }

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.



#7 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 25 September 2006 - 02:47 PM

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.

Info: PHP Manual


#8 duncanhall

duncanhall
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 25 September 2006 - 03:01 PM

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.

  • 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.

Will this session variable remain set regardless of refreshes or site navigation?




#9 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 25 September 2006 - 03:40 PM

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.

Info: PHP Manual





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users