Jump to content

PHP / AJAX Help?


duncanhall

Recommended Posts

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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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 [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?
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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?


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.