views:

52

answers:

3

I'm not sure if this is even possible or not, and I can't quite figure out how to actually do it, if possible at all.

What I'd like to do is have a list of checkboxes get submitted to an .ajax() call. That .ajax() call passes the information to PHP for processing (and in this case it will be sending e-mails to each one of those IDs selected).

However, I don't want the page (or the .ajax() call for that matter) to wait for the PHP script to finish running. Basically I want it to hand off the data and move on (and display a .dialog() box, etc., etc.)

Here's the trick though: I do want some type of feedback from the PHP script. When it's done, I want it to return data to the user, possibly by displaying a bar at the top of the page (much like what StackOverflow does when you have system messages, they show up at the top of the page regardless of where you are on the site.)

The first part, having a list of checkboxes displayed is done. I'm as far ahead as determining which ones are checked and collect that data. Now I need to figure out how to pass it to the .ajax() call in such a way that PHP can use it to scan a database to collect the various e-mails and start sending messages out. Not quite sure how to pass the data. It seems silly to create a dataString that consists of id1=XXX&id2=XXX&id3=XXX-and_so_forth.

I'm open for suggestions here.

[Edit] Anyone have any suggestions for the part I made bold above?

A: 

THen you can go a head and use asyncronous ajax call , whatever logic you keep inside the success method of ajax can be moved out.

MakeAjaxCall() don't write anything inside the success method.

Write all your logic after you makeAjaxCall() ,so that both happens in parallel.

makeAjaxCall() , instantiates the process doyourlogicInParallel()...here

gov
Thanks! I had run into this not too long ago and didn't put it together. At 4am, the brain is a bit ... slow ...
KirAsh4
+4  A: 

Hi,

default behavior for the Ajax Request is that they are asynchronous and that implies that the code run normally and don't wait the return of the AJAX call.

Example:

$.ajax({
    url: "...",
    data: "<checkboxes>",
    success: function(data, xhr, status) {
        // Code to add your banner when AJAX finished
    }
});

// Inline code to execute whitout wait the end of $.ajax
Arnaud F.
Why didn't I put 2 and 2 together ... I ran into this not too long ago, making an .ajax() call and having things process after the call, but because the processing routine was outside of the .ajac() call, it was always returning zero. Eventually I figured out I needed to put it inside of the .ajax() call to have it process properly. That should've been my clue! Put it outside, it processes in parallel ...
KirAsh4
Question though, if someone browses away from the page (but remains on the site), it will re-initialize all of my js stuff (it reloads the main js file which contains everything), so how would the .ajax() call know what to do when it receives something back from PHP?
KirAsh4
+1 also you can set a parameter `async:` to the `ajax` request.
Gutzofter
In PHP you can use $_SETTING variable to store the response that gets sent to client. Open another question.
Gutzofter
A: 
 If you are making calls to a .php script with AJAX (xmlHttpReq) and use 
 flush, this will send data to your script HOWEVER it will not set the 
 xmlHttpReq.readyState to 4 -- which is a requirement to use the information 
 sent (Firefox does however allow you to use the responseText property with a
 readyState == 3 but IE will throw an error).

I got this from PHP.net when reading up on the flush() - http://www.php.net/manual/en/function.flush.php

Basically this means that you can have a process started by an ajax call and have it return some value for example echo "hello"; but not have the readystate set to 4. So if you are clever about it you can manipulate your responses from your PHP script like for instance in a while loop with a timer.

etbal
this is an interesting approach for getting output back even if it seems like the request is done.
etbal
Actually, I have not been relying on readyState. All of my PHP calls will return an integer, string, or valid XML, and my JS calls will parse that. For example, a simple 'yes/no' DB call will return 0 or 1, and the JS acts according to that return. In the past I've added custom errors to the PHP script which got returned to the ajax call (E_LOGIN, E_QUERY, dumb things like that.)
KirAsh4