0

I'm having a little problem with jquery with php combination. Let me begin with the codes.

PHP Code :

if ($_POST["request"]==10)
    progress();

function progress()
{
    $files = getfiles();
    $progress = 0;
    $progressRate = 100/$files->number;
    for ($i=0;$i<$files->number;$i++)
    {
        sendfile($files->location[$i]);
        $progress+=$progressRate;
        echo $progress;
    }
}

Code Output : For example files->number = 2 then output will be 50 100.

Jquery Code :

jQuery.post("functions.php",{request:10},function(data){

            alert(data);


   });

Problem : What I want is to alert 50 first, then alert 100 after, I mean when the first loop ends in php and echos the first progress value, then jquery should be able to intercept it, but jquery alerts the data after the whole loop finishes, which means I get 50100 in one single alert, is there any way jQuery can handle this? I know there are other solutions using PHP and generating for example the number of loops in a hidden div or an attribute, then using loops inside the jQuery, But I really want to do it directly.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Faouzi FJTech
  • 981
  • 3
  • 13
  • 27
  • @PeeHaa the close-parenthesis is misplaced, but OP says it's working, so we should take his word on it. The issue is that he's trying to access a single response as multiple responses, which won't work. – Matt Aug 20 '12 at 20:43
  • @PeeHaa what isn't valid php? That code bit is perfectly valid on any php server – Pierre Aug 20 '12 at 20:44
  • The code I wrote it just now, because it's not my real one, I simplified it to make the issue simpler, here the important is how jquery handles the output, not how the output was created – Faouzi FJTech Aug 20 '12 at 20:44
  • 5
    @Pierre So you are telling me that `if ($_POST["request"])==10` isn't producing an syntax error where you live? That would be... strange – PeeHaa Aug 20 '12 at 20:45
  • @Pierre, the `if` statement is bad. – Matt Aug 20 '12 at 20:45
  • 2
    Guyz I fixed the if statement, it is not what matters, the output is what matters, thank you – Faouzi FJTech Aug 20 '12 at 20:47
  • @PeeHaa oops I totally missed that little bit, I was looking much deeper for errors, sorry guys – Pierre Aug 20 '12 at 20:48
  • When you echo something back, the connection is closed as the call is finished. If you really need full duplex communication with the server you need to look into things like websockets. – adeneo Aug 20 '12 at 20:51

2 Answers2

5

That's not how AJAX works. It makes a single request and does "something" with the response, just like the browser does when you navigate to a URL.

The PHP script will execute server-side, print the results to the "screen" and send them back as a response.

If you want multiple variables returned, you should return them as JSON data and parse the JSON string in javascript.

Matt
  • 6,993
  • 4
  • 29
  • 50
  • The problem is that these values, will update a progress bar, so I need to get the output when its generated, not wait for everything to be finished, else it won't be useful – Faouzi FJTech Aug 20 '12 at 20:48
  • @FaouziNikolaic Then you need to use a different approach. The request-response pattern won't do this for you. – Matt Aug 20 '12 at 20:49
  • @FaouziNikolaic and before you ask, no I don't know how to accomplish this; I've never had to do it before. But I *do* know that you're using the wrong approach. – Matt Aug 20 '12 at 20:52
  • Thank you for the kind answer ! let's see if someone can solve it using this approach because I know I can solve it using a php trick, but jQuery one would be simpler. – Faouzi FJTech Aug 20 '12 at 20:56
0

jQuery (actually AJAX by default) only returns the response once the server has completed the request.

So there is no way in doing a single request to a php page and getting multiple responses. One way is to use php with the upload progress support, but I doubt that will actually help in your situation as it is only for uploading files.

Pierre
  • 1,553
  • 12
  • 22
  • Yeah, Im uploading files to google drive so it won't work, but thank you for the input I didn't know this upload progress existed in PHP for uploading files. – Faouzi FJTech Aug 20 '12 at 20:59
  • a workaround could be doing an ajax request to a page that returns a list of all the files, looping through that list in javascript and doing an ajax request on each file. Might be overkill if you have lots of files, but you can change the progress bar after every successful ajax request, or you can use web sockets, but it may take a lot of extra coding to accomplish this, and web sockets isn't supported by all the browsers. I have also had many situations where I needed this, and every time I had to use a workaround with lots of code or scrape the idea and do something else. – Pierre Aug 20 '12 at 21:09
  • Yep Thank you Pierre, That's the same alternative I talked about in the post, It involves some more work but I guess this is the only one I can do if no ajax request can handle this. – Faouzi FJTech Aug 20 '12 at 21:14