1

Is it possible to have a php script pause at a certain point wait for an ajax response, then continue executing?

If not, what is the most appropriate way to store the current state of a script (all defined variables ect) untill it in invoked again?

Hailwood
  • 89,623
  • 107
  • 270
  • 423
  • 2
    A *PHP* script to wait for an *Ajax* response? I don't understand. – Pekka Jan 13 '11 at 23:22
  • i think he means that while his page is being printed out, he wants teh page to make an ajax call, but not continue printing out until the response has been received... something like that anyways. – dqhendricks Jan 13 '11 at 23:24
  • 3
    @haliwood You need to research how HTTP requests work, and get a clear understanding of what things happen on the client, and what happens on the server. You will have a much easier time after that. – profitphp Jan 13 '11 at 23:30

4 Answers4

1

Making it wait for an ajax response which could never come sounds like a very bad plan (apart from being impossible due to the nature of http as a stateless protocol).

What's wrong with using session to persist user data (you can store objects in the session)? It won't serialize the whole state automagically though.

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • There is no way to 'make it wait'. Once its sent to the client (where 'ajax' happens), php is done processing (buffering excluded), and can get no new input. – profitphp Jan 13 '11 at 23:33
  • Just to be clear: there is a hack that would allow you to do this (using fork), so it is possible. It is just very very very bad, ugly, inefficient and insecure hack - but not impossible. :) – johndodo Aug 10 '11 at 13:31
0

Not how php works, or ajax really either. Each request is independent. You can use a session.

profitphp
  • 8,104
  • 2
  • 28
  • 21
0

I think you are looking for Cometlink text - which is a form of reverse Ajax which holds the connection open indefinitely - although I don't think you'd have a PHP script just wait for it...

HorusKol
  • 8,375
  • 10
  • 51
  • 92
  • Again, that doesn't happen when PHP is running, with comet the httpxmlrequest from javascript just never closes the connection, rather than refreshing it incessantly, which is iffy given browser settings, proxies, etc not liking connections which never close. Regardless, comet is not anything where php can wait for something to happen on the client after its already done outputting. – profitphp Jan 13 '11 at 23:39
  • true - but the question is if the script 'needs' to wait in the middle, or if the server can be written in such a way as to respond to a comet request - since there is no way to do what the OP is asking, I'm just suggesting a method to come close to it. – HorusKol Jan 13 '11 at 23:45
0

If you want to get in your php script response that you would normally access via ajax then just use curl functions. Curl will wait for the response to arrive.

Kamil Szot
  • 17,436
  • 6
  • 62
  • 65