Forum Moderators: coopster

Message Too Old, No Replies

PHP Function echo'ing data back to AJAX and storing data to database

asynchronous php function ajax echo json_encode onComplete

         

theChronic

11:58 am on Mar 29, 2011 (gmt 0)

10+ Year Member



Hey WebmasterWorlders

I had a quick question that I was hoping someone could briefly help me out with or point me in the right direction. It's about running a PHP function simultaenously (or asynchronously I guess?) as it also echo's data back to the AJAX that called it.

I'd like to make an AJAX call to my Controller that gets some JSON data from a page. Then with that data I would like to send it back to the AJAX by echo json_encode()ing it but I'd also like to send that data to another PHP function that stores the data to the database.

My concern is that if I echo'd the information back and then sent the data to the storing function, the AJAX would not register onComplete until the function reached its return; after sending the information.

So our onComplete wouldn't do anything until the info was received and stored but I'd like it to be done as soon as the info is received.

Any helps, pointers, or insight would be a great help.

Thanks
John

omoutop

1:41 pm on Mar 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




I'd like to make an AJAX call to my Controller that gets some JSON data from a page. Then with that data I would like to send it back to the AJAX by echo json_encode()ing it but I'd also like to send that data to another PHP function that stores the data to the database


why dont you save your data (sent them to your function) before echoing them the first time?

theChronic

8:22 pm on Mar 29, 2011 (gmt 0)

10+ Year Member



Because wouldn't the data not be echo'd to the user until that function is complete? And so if storing the data takes much time, it will delay the user from getting to see it.

coopster

4:58 pm on Mar 30, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could try flushing [php.net] the buffer but I think you are still going to have problems because the connection will still be open. If you are concerned about the process taking too much time you could spawn the process instead, or if you are writing to the database you could use a delayed insert. MySQL supports delayed inserts [dev.mysql.com]

If I was you though, I would try what omoutop recommended first and test it. I would be very surprised if you have any substantial delay.