Wait function that receives data in PHP

人盡茶涼 提交于 2019-12-12 04:38:42

问题


Can you send me in the right direction for writing a Wait function in PHP? I'm using procedural code style.

The scenario is as following:

  1. First, my webapp sends data to a server, and then it should pause the execution on the sending client (the webapp).
  2. Second, the server receives the data and does something to that data, and it sends the processed data back to the sender of step 1 (the webapp).
  3. Third, the sender of step 1 (the webap) receives the processed data from the server and checks it whether it's true.

I need guidance in writing the wait and resume function.


回答1:


This defentely sounds like you need to use AJAX. jQuery gives you some comfortable ways to solve your Problem.

$.post( "test.php", { name: "John", time: "2pm" })
.done(function( data ) {
alert( "Data Loaded: " + data );
});

This is a code example from https://api.jquery.com/jQuery.post/. You might have a look at https://api.jquery.com/jQuery.ajax/.

This AJAX function calls the script test.php with the given parameters as POST variables. You will recieve an answer (data), which you can use in your JS afterwards. I think the done() function does exactly what you are asking for. To have some consistency between the languages you could work with JSON objects.

You cannot actually stop PHP from executing. Thats why you will use a certain file which handles your request.




回答2:


The people here said that Ajax is needed to do this. This is true. But, it's not the only option available. Using cURL is an option too, and having CURLOPT_RETURNTRANSFER (which returns the value after the transfer) set it does what I needed to do:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


来源:https://stackoverflow.com/questions/22512083/wait-function-that-receives-data-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!