acidglitter Posted May 2, 2008 Share Posted May 2, 2008 what i'm doing right now is using GET to open a php file. then whatever is echoed i get to show up with like objectName.responseText. but now i want to update two different things.. how can you have more than one response from only one php file? Quote Link to comment Share on other sites More sharing options...
wrongmove18 Posted May 2, 2008 Share Posted May 2, 2008 Can't understand what you want to do.. But as for my understanding, do you want 2 replies in one request? Correct me if I'm wrong... In AJAX, 1 request = 1 reply only. Here are some options that I can recommend: 1. Call another request after the first one. 2. Have a delimiter in your responseText to separate the reply. Quote Link to comment Share on other sites More sharing options...
acidglitter Posted May 2, 2008 Author Share Posted May 2, 2008 yea thats what i'm trying to do. how would i be able to put a delimiter in the responseText? Quote Link to comment Share on other sites More sharing options...
dungareez Posted May 3, 2008 Share Posted May 3, 2008 If what you want to do is update multiple DIVS for example, you can use a unique string to separate div name from value. IE here is an onFinish function I used to process multiple divs from one responseText. I am sure pro coders will cringe at this but it worked for me. I am sure you could also use json or XML responses to do it as well... function onFinish(req) { var result = req.responseText; var resultArray = result.split("_____"); //splits off the first part(can be repeated for multiple divs) of the array which contains the div that is to be updated, _____ is just a unique character combination var max_i = resultArray.length; //this loop cycles through the array and to grab each div ID and update the content associated with it for (var i=0; i<=max_i; i = i+2) { var h = i+1 ; //used to set array position for data to fill selected div var div = resultArray; //the div to be updated var displayResult = resultArray[h]; // takes the second part of the response that will be displayed in the desired div $(div).innerHTML= displayResult; //display result in the selected div } } What this does is takes a text response say for example, "div1_____junk to put in div1_____div2_____junk to put in div2" and will split it into an array in javascript by separating at the "_____". Then it loops through the array taking two array locations at a time, var h and var i. these will be the div name and div value respectively. It then uses these values to update the divs and incremnts up by two to get the next two values in the array if there are more. Hope this helps. Like I said I am a beginner myself, so I am sure there are many better ways of accomplishing this, but for now, it works. Good luck. Quote Link to comment Share on other sites More sharing options...
acidglitter Posted May 5, 2008 Author Share Posted May 5, 2008 thanks that would work perfectly! i know how to do that with php (explode()) but i'm really new to javascript. Quote Link to comment Share on other sites More sharing options...
wrongmove18 Posted May 6, 2008 Share Posted May 6, 2008 explode() in javascript is split(); Format: String.split('delimiter'); Example: var str = "string1:string2"; var output = str.split(":"); Quote Link to comment Share on other sites More sharing options...
pingu Posted May 13, 2008 Share Posted May 13, 2008 Sorry to hijack... What about if you did want two different responses, namely, one from one PHP script and one from another. How would one go about making two separate requests from the same function without one response overwriting the other in the HTTPObj? I'm guessing that you would need to create a new request object, I can't figure out how to create a separate instance of the object. Cheers Dan Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 13, 2008 Share Posted May 13, 2008 You are correct, you would need another XHR object. This might help: http://javascript.about.com/library/blajax13.htm One thing to keep in mind when using XHR is that a browser can only have two simultaneous requests active at a time. Hence more complicated XHR programs don't invoke the request immediately when the client code makes the call. Instead, the parameters of the request are stored in a priority queue with automatic promotion and another piece of code dequeues and invokes the requests as resources are available. For example, say the client code wants to make 5 simultaneous XHR requests A, B, C, D, and E with priorities 1, 2, 4, 2, and 1 respectively. Assuming lower numbers represent high priority, these requests go into a queue: (A,1), (E,1), (B,2), (D,2), and (C,4) The browser can only ever have two requests open at a time. So the XHR server-code keeps track of the number of requests currently active: var n_xhr_requests = 0; We have zero requests so we can dequeue a request and invoke it; this removes (A,1) and increments n_xhr_requests to 1. We have one request so we can dequeue another and invoke it; this removes (E,1) and increments n_xhr_request to 2. We are using all our requests so we stop dequeuing. Meanwhile, request (F,3) is enqueued, changing our queue to: (B,2), (D,2), (F,3), and (C,4) Request (E,1) returns so we can dequeue the next one (B,2) and invoke it. Let's assume requests with a priority of 1 through 3 inclusive keep coming in. They will always be ahead of (C,4) so our (C,4) request will never happen. This is where the automatic promotion behavior of a priority queue comes in handy. If any item sits in the queue for too long, it's priority is automatically increased. Hence after some duration of time, (C,4) will change to (C,3). If (C,3) happens to sit in there long enough again, it will be promoted to (C,2). Eventually via automatic promotion C will be invoked. Fun stuff, eh? Quote Link to comment Share on other sites More sharing options...
pingu Posted May 14, 2008 Share Posted May 14, 2008 Many thanks, I think I get the theory. Any chance you could post a bit of pseudo (or real) code to illustrate it? Give me something to get my teeth into. Cheers Dan Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 14, 2008 Share Posted May 14, 2008 That would be a lengthy post in and of itself. I might do something like that for my blog; if I do I'll update this thread to point back there. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted May 14, 2008 Share Posted May 14, 2008 try looking up some json remote examples. what you do with json is calling php to return an js array which is then usuable on your mainpage. because its javascript you can easily update variable things Quote Link to comment 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.