How to call a function asynchronously in php
-
I want to execute these functions when user click on button but I want to execute $model->user->email ? $model->sendUserMail(): ""; that function asynchronously in background while other function will execute as click occur Here is my code if (isset($_POST['create_send'])) { if ($model->save()) { $model->order_id = strtotime(date("Y-m-d H:i:s")) + '' + $model->id; // send mail $model->sendMail(); //send sms $messages = $model->sendSMSMessage($model); // create booking history $model->createBookingHistory($model , $messages); $model->user->email ? $model->sendUserMail(): ""; if($model->booking_stage != 'inprogress'){ $this->sendPushToSeller($model); } $model->update(); $this->redirect(array('index', 'id' => $model->id)); } }
-
Answer:
There is nothing like Async Tasks in Standard PHP. You could install pthreads or you have to work with some sort of a Queue. With a Queue it would work like that: Create a Mail-Send-Job Push that Job into a Queue (for example a Database) A third party programm (for example a cronjob) has to poll the Queue and execute the Tasks (send the Mail) if there are some Or like @Alan Machado said, use AJAX. So your page can load, and the Buttons sends an AJAX Request to the Server that sends the Mail.
Himanshu Gupta at Stack Overflow Visit the source
Other answers
You tagged it yii, so perhaps consider a RESTful API extention, and research AJAX and CRUD. It depends on how far you want to take it. But I feel a RESTful API with yii is the best way to go when it comes to AJAX.
turbopipp
Related Q & A:
- How to call jQuery function with multiple parameters?Best solution by Stack Overflow
- How to call a function with parameter in a bash script?Best solution by tldp.org
- How to fire a function before and after automatically in a jquery plugin?Best solution by catchmyfame.com
- how to call a function in Python in another function?Best solution by Yahoo! Answers
- How to call Javascript function in PHP?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.